gpt4 book ai didi

JavaScript 对象尺寸

转载 作者:行者123 更新时间:2023-12-02 16:06:10 25 4
gpt4 key购买 nike

我想定义一个二维对象,但没有成功。

var b = [1,2,3,4,5,6,7,8];
var a = {};
var c = ["baslik","resim","icerik","links"];

for(i=0;i<2;i++){
for(s=0;s<4;s++){
a[c[s]]=b[s];
}
}

document.writeln(JSON.stringify(a));

输出:

{"baslik":1,"resim":2,"icerik":3,"links":4}

我想要这个输出:

{"baslik":1,"resim":2,"icerik":3,"links":4},  
{"baslik":5,"resim":6,"icerik":7,"links":8}

最佳答案

尝试:

var a = []; //Must be a array
var b = [1,2,3,4,5,6,7,8];
var c = ["baslik","resim","icerik","links"];

for(i=0;i<2;i++){
var obj = {} //Object
for(s=0;s<4;s++){
obj[c[s]]=b[s+4*i];
}

a.push(obj)
}

document.writeln(JSON.stringify(a));

关于JavaScript 对象尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30700369/

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