gpt4 book ai didi

javascript - 将数组编号 2 和 3 访问到一个字符串数组中

转载 作者:数据小太阳 更新时间:2023-10-29 05:47:32 26 4
gpt4 key购买 nike

如何输出:

ID: 0001
Name: Mike
Birthday: London 21/05/1989
Hobby: Reading

我下面的代码是未定义的,我希望数组 city + date 在生日时在一起。

我的代码不是,请检查下面的代码:

var input = [
["0001", "Mike", "London", "21/05/1989", "Reading"],
["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
["0003", "John", "Kansas", "25/12/1965", "Cooking"],
["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
];

var data = ["ID: ", "Name: ", "Birthday: ", "Hobby: "];




for(var i = 0 ; i <= input.length ; i++){
for(var j = 0 ; j <= input.length ; j++){
for(var i = 0 ; i <= data.length; i++){
console.log(data[i] + input[j][i])
};
};
};

是否有任何修复此逻辑的建议?为此,我只想使用循环。

最佳答案

您可以使用 array.map

var input = [
["0001", "Mike", "London", "21/05/1989", "Reading"],
["0002", "Sara", "Manchester", "10/10/1992", "Swimming"],
["0003", "John", "Kansas", "25/12/1965", "Cooking"],
["0004", "Dave", "Nevada", "6/4/1970", "going to gym"]
];

var expectedOutput = input.map(a=>{
return {ID:a[0],Name:a[1],Birthday:a[2] + ' ' + a[3],Hobby:a[4]}
})

console.log('string output',JSON.stringify(expectedOutput));
console.log(expectedOutput);

关于javascript - 将数组编号 2 和 3 访问到一个字符串数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53042611/

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