gpt4 book ai didi

javascript - 将普通数组转换为html中的对象数组

转载 作者:行者123 更新时间:2023-11-30 15:59:13 25 4
gpt4 key购买 nike

我有下面的数组列表:

var number = [];
var counter = [];
var time = [];

number[0] = "1212";
counter[0] = "2";
time[0] = "12:30PM";

number[1] = "9367";
counter[1] = "4";
time[1] = "05:30PM";

number[2] = "4786";
counter[2] = "8";
time[2] = "09:30AM";

如何将上面的内容转换成如下所示的对象数组:

var abc = [
{number: "1212", counter: "2", time: "12:30PM"},
{number: "9367", counter: "4", time: "05:30PM"},
{number: "4786", counter: "8", time: "09:30AM"}
];

到目前为止,我尝试使用以下代码进行转换但失败了:

var abc=[];
abc[0].number = "1212";
abc[0].counter = "2";
abc[0].time = "12:30PM";

你们知道怎么做吗?

最佳答案

abc[0] 初始化为一个对象,然后更新它的属性。

var abc = [];
abc[0] = {};
abc[0].number = "1212";
abc[0].counter = "2";
abc[0].time = "12:30PM";

console.log(abc);

或者用所有对象属性初始化它。

var abc = [];
abc[0] = {
number: "1212",
counter: "2",
time: "12:30PM"
}

console.log(abc);

关于javascript - 将普通数组转换为html中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025917/

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