0

I have a json given below

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"}

The goal here is to get the total number of booked_cars on a time. So according to this json. I should get

4 bookings for 9:00 AM and 5 bookings for 10:00 AM.

How do I achieve that?

3
  • I think the issue is that you are assigning = the value, so it always replaces, you could try creating the object you need using map Commented Nov 1, 2017 at 20:27
  • window.confirmedBookings has a length property, so it's an Array, yes? Commented Nov 1, 2017 at 20:29
  • @Roamer-1888 Yes Commented Nov 1, 2017 at 20:33

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

Comments

2

You could change it to something like that.

var values = window.confirmedBookings.reduce(function(prev, current) {
    if(!prev[current.sharing_schedule]) prev[current.sharing_schedule] = 0;
    prev[current.sharing_schedule] += Number(current.booked_cars);
    return prev;
}, {})

@edit: updated an answer to fit a question.

2 Comments

Its not necessary that I use an object, we can use an array too. I just need to make it in the given order
Alright, now it makes more sense. I'll update my answer in a minute.
0

You appear to be asking for :

window.totalBooked = {};
$.each(window.confirmedBookings, function(index, value) {
    console.log(value.booked_cars);
window.totalBooked[value.sharing_schedule] = (window.totalBooked[value.sharing_schedule] || 0) + +value.booked_cars;
});

or, slightly simpler :

window.totalBooked = window.confirmedBookings.reduce(function(obj, value) {
    obj[value.sharing_schedule] = (obj[value.sharing_schedule] || 0) + +value.booked_cars;
    return obj;
}, {});

DEMO

1 Comment

Check the question again mate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.