gpt4 book ai didi

JavaScript 数组和 JSON

转载 作者:行者123 更新时间:2023-12-03 06:24:40 24 4
gpt4 key购买 nike

我正在研究 Bootstrap calendar 并且我试图让我的数组看起来像这样:

{
"success": 1,
"result": [
{
"id": "293",
"title": "This is warning class event with very long title to check how it fits to evet in day view",
"url": "http://www.example.com/",
"class": "event-warning",
"start": "1362938400000",
"end": "1363197686300"
},
{
"id": "256",
"title": "Event that ends on timeline",
"url": "http://www.example.com/",
"class": "event-warning",
"start": "1363155300000",
"end": "1363227600000"
},
{
"id": "276",
"title": "Short day event",
"url": "http://www.example.com/",
"class": "event-success",
"start": "1363245600000",
"end": "1363252200000"
},
{
"id": "294",
"title": "This is information class ",
"url": "http://www.example.com/",
"class": "event-info",
"start": "1363111200000",
"end": "1363284086400"
}
] }

但是我尝试过并得到了这样的结果:

[  
{
"success":1
},
{
"result":[
{
"id":"1",
"title":"dev",
"url":"www.example.com",
"class":"event-success",
"start":"0",
"end":"0"
},
{
"id":"2",
"title":"Holiday",
"url":"www.example.com",
"class":"event-success",
"start":"0",
"end":"0"
},
{
"id":"3",
"title":"Coding...",
"url":"www.example.com",
"class":"event-success",
"start":"0",
"end":"0"
},
{
"id":"4",
"title":"data",
"url":"www.example.com",
"class":"event-success",
"start":"0",
"end":"0"
}
]
}
]

我创建这些困惑的代码是

      var result = [];
for(i=0; i<rows.length; i++) {
var result_single = {id:rows[i].uid, title:rows[i].name, url:"www.example.com", class:"event-success", start:rows[i].startdate, end:rows[i].enddate};
result.push(result_single);
}
var output = new Array();
output.push(data);
result = {result:result};
output.push(result);
console.log(JSON.stringify(output));

说实话,我不太明白js中的数组是如何工作的。有人可以指出我是怎么出错的吗?

最佳答案

您的预期结果是一个包含 2 个属性的对象,但您得到一个包含两个对象的数组。我认为您想要第一个结果,它是具有两个属性的对象。

var result = [];
var rows = [1,2,3,4,5];

for(i=0; i<rows.length; i++) {
var result_single = {id:rows[i], title:rows[i], url:"www.example.com", class:"event-success", start:rows[i].startdate, end:rows[i].enddate};
result.push(result_single);
}

var output = {
success: 1,
result: result
}

console.log(JSON.stringify(output));

关于JavaScript 数组和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711771/

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