gpt4 book ai didi

javascript - 格式化数组以在 node.js 中输出

转载 作者:行者123 更新时间:2023-11-30 08:29:03 24 4
gpt4 key购买 nike

var toFormat = [[1, "one", "unu"], [2, "two", "du"], [3, "three", "tri"], [4, "four", "kvar"]];

我需要输出数组 toFormat 所以它应该是这样的:

1 (one)
2 (two)
3 (three)
4 (four)

(每个子数组的第三个元素不用)如何做到这一点?

编辑:循环在这里

var res = []; 
for(var i=0;i<toFormat.length;i++){
res.push(toFormat[i][1]+" ("+toFormat[i][2]+")");
}
console.log(res.join("\n"))

最佳答案

const toFormat = [
[1, "one", "unu"],
[2, "two", "du"],
[3, "three", "tri"],
[4, "four", "kvar"]];

const result = toFormat.map(([val, string]) => `${val} (${string})`).join('\n');

console.log(result);

关于javascript - 格式化数组以在 node.js 中输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40728599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com