gpt4 book ai didi

javascript - 将两个 json 对象数据合并为一个,JSON 为新的 Single JSON,

转载 作者:行者123 更新时间:2023-11-28 11:11:20 24 4
gpt4 key购买 nike

我有这个对象,定义如下:

var object = {
"driver_data": [{
"slot": 0,
"rideCount":98
}, {
"slot": 30,
"rideCount": 75
}, {
"slot": 100,
"rideCount": 0
}],
"passenger_data": [{
"slot": 0,
"rideCount":33
}, {
"slot": 30,
"rideCount": 56
}, {
"slot": 100,
"rideCount": 37
}]
}

想要转换为:

[{"Slot":0,"passenger_data":98,"driver_data":33},
{"Slot":30,"passenger_data":75,"driver_data":56},
{"Slot":100,"passenger_data":0,"driver_data":37}
]

我尝试过这个但行不通。

var slot1 = [],
Dride = [],
Pride = [];
var object = {
"driver_data": [{
"slot": 0,
"rideCount": 98
}, {
"slot": 30,
"rideCount": 75
}, {
"slot": 100,
"rideCount": 0
}],
"passenger_data": [{
"slot": 0,
"rideCount": 33
}, {
"slot": 30,
"rideCount": 56
}, {
"slot": 100,
"rideCount": 37
}]
}
object.driver_data.forEach(function(key) {
slot1.push(key.slot);
Pride.push(key.rideCount)
});
object.passenger_data.forEach(function(key) {
Dride.push(key.rideCount);
});
var myObj = {},
myObj1 = {},
myObj2 = {},
monk=['Slot','passenger_data','driver_data'];
var arr = [];
for(var i=0;i<3;i++){
myObj[monk[i]] = slot1[i];
myObj1[monk[i]] = Pride[i];
myObj2[monk[i]] = Dride[i];
}
arr.push(myObj, myObj1, myObj2)
console.log(JSON.stringify(arr))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

最佳答案

假设 driver_datapassenger_dataslot 的顺序相同,您可以在 mapdriver_data像这样:

var obj = {"driver_data":[{"slot":0,"rideCount":98},{"slot":30,"rideCount":75},{"slot":100,"rideCount":0}],"passenger_data":[{"slot":0,"rideCount":33},{"slot":30,"rideCount":56},{"slot":100,"rideCount":37}]}

let output = obj.driver_data.map((d, index) => ({
Slot: slot,
driver_data: d.rideCount,
passenger_data: obj.passenger_data[index].rideCount
}))

console.log(output)

如果slot的顺序不同,您可以使用mapfind像这样。获取与司机具有相同槽位乘客。返回具有计数值的新对象

var obj={"driver_data":[{"slot":0,"rideCount":98},{"slot":30,"rideCount":75},{"slot":100,"rideCount":0}],"passenger_data":[{"slot":0,"rideCount":33},{"slot":30,"rideCount":56},{"slot":100,"rideCount":37}]}

let output = obj.driver_data.map(d => {
let passenger = obj.passenger_data.find(p => p.slot === d.slot);
return {
Slot: d.slot,
driver_data: d.rideCount,
passenger_data: (passenger || {}).rideCount
}
})

console.log(output)

或者使用Array.from (假设插槽按顺序排列)

var obj={"driver_data":[{"slot":0,"rideCount":98},{"slot":30,"rideCount":75},{"slot":100,"rideCount":0}],"passenger_data":[{"slot":0,"rideCount":33},{"slot":30,"rideCount":56},{"slot":100,"rideCount":37}]}

let { driver_data, passenger_data } = obj;

const output = Array.from({ length: driver_data.length }, (_, i) => {
return {
Slot: driver_data[i].slot,
driver_data: driver_data[i].rideCount,
passenger_data: passenger_data[i].rideCount
}
})

console.log(output)

关于javascript - 将两个 json 对象数据合并为一个,JSON 为新的 Single JSON,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55235657/

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