gpt4 book ai didi

javascript - 如何将一个对象json转换为数组对象json?

转载 作者:行者123 更新时间:2023-11-30 09:49:17 25 4
gpt4 key购买 nike

我的对象json是这样的:

var a = [{
"attributes": {
"Code": "SGL",
"Total": "811400"
},
"DayPrice": {
"Date": "2016-07-22",
"Rate": "811400"
}
}];

我想将 DayPrice 更改为这样的数组:

var a = [{
"attributes": {
"Code": "SGL",
"Total": "811400"
},
"DayPrice": [{
"Date": "2016-07-22",
"Rate": "811400"
}]
}];

有什么办法可以解决我的问题吗?

非常感谢

最佳答案

您需要遍历对象数组并将每个对象的 DayPrice 属性包装在一个数组中,如下所示:

for (var i = 0; i < a.length; i++) {
a[i].DayPrice = [a[i].DayPrice];
}

Working example

关于javascript - 如何将一个对象json转换为数组对象json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37271270/

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