gpt4 book ai didi

javascript - jQuery 添加数组值

转载 作者:行者123 更新时间:2023-11-28 14:44:12 27 4
gpt4 key购买 nike

下面给出了一个 json

0 : {car_rental_id: "6007", sharing_schedule: "9:00 AM", booked_cars: '1', woo_order_id: "6421", woo_status: "on-hold" }
1 : {car_rental_id: "6007", sharing_schedule: "9:00 AM", booked_cars: '3', woo_order_id: "6424", woo_status: "pending" }
2 : {car_rental_id: "6007", sharing_schedule: "10:00 AM", booked_cars: '5', woo_order_id: "6427", woo_status: "pending"}

这里的目标是获取一次booked_cars的总数。所以根据这个json。我应该得到

上午 9:00 有 4 个预订,上午 10:00 有 5 个预订。

我如何实现这一目标?

最佳答案

var test = [{
car_rental_id: "6007",
sharing_schedule: "9:00 AM",
woo_order_id: "6421",
woo_status: "on-hold",
booked_cars: '1'
}, {
car_rental_id: "6007",
sharing_schedule: "9:00 AM",
woo_order_id: "6424",
woo_status: "pending",
booked_cars: '3'
}, {
car_rental_id: "6007",
sharing_schedule: "10:00 AM",
woo_order_id: "6427",
woo_status: "pending",
booked_cars: '5'
}];

var carsBookedByTime = test.reduce(function(collection, element){
var bookedCars = parseInt(element.booked_cars);
//initialize total in the collection if it does not exist
if (!collection[element.sharing_schedule]) collection[element.sharing_schedule] = 0;

collection[element.sharing_schedule] += bookedCars;

return collection;
}, {});

console.log(carsBookedByTime);

关于javascript - jQuery 添加数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47062969/

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