gpt4 book ai didi

javascript - 从具有许多记录的 JSON 结果创建 JSON 结构

转载 作者:行者123 更新时间:2023-11-28 06:54:07 25 4
gpt4 key购买 nike

friend 们,进一步回答我关于Creating JSON structure from JSON results的问题如何创建包含数组中许多 header 的对象?

Here is my Fiddle我的代码如下。

var array = [
[{
"label": "NewNetworkServiceProvider",
"value": "NewNetworkServiceProvidered46c4ee-7ec1-45d6-9d13-94e301d2f890"
}, {
"label": "PurchaseOrderNumber",
"value": "PurchaseOrderNumber4be9f460-0c98-4038-910d-027565f83e1c"
}, {
"label": "RawRecordType",
"value": "RawRecordType2a774afb-0fd4-4fd4-a3c6-88041de5b1ad"
}],
[{
"label": "NewNetworkServiceProvider",
"value": "NewNetworkServiceProvidered46c4ee-7ec1-45d6-9d13-94e301d2f890"
}, {
"label": "PurchaseOrderNumber",
"value": "PurchaseOrderNumber4be9f460-0c98-4038-910d-027565f83e1c"
}, {
"label": "RawRecordType",
"value": "RawRecordType2a774afb-0fd4-4fd4-a3c6-88041de5m321"
}]
];

var obj = {
header: {}
};
array.forEach(function (item) {
item.forEach(function (data) {
obj.header[data.label] = data.value;
});
});

console.log(obj)

最佳答案

我猜你想要这个:

array.forEach(function (item) {
item.forEach(function (data) {
obj.header[data.label] = obj.header[data.label] || [];
obj.header[data.label].push(data.value);
});
});

http://jsfiddle.net/867mb14s/1/

关于javascript - 从具有许多记录的 JSON 结果创建 JSON 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32708041/

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