0

I know this might be an easy task though I am struggling quite a lot with this one:

I have an Array of objects looking like this:

[{date: '01-01-2022' , count: 1},
 {date: '02-01-2022' , count: 2},
 {date: '05-01-2022' , count: 9}]

My expected outcome would be:

[{date: '01-01-2022' , count: 1 , sum: 1},
 {date: '02-01-2022' , count: 2 , sum: 3},
 {date: '05-01-2022' , count: 9 , sum: 12}]

or alternatively:

[{date: '01-01-2022' , count: 1},
 {date: '02-01-2022' , count: 3},
 {date: '05-01-2022' , count: 12}]

I can sum up the count array using

    let new_array = [];  
    myarray.reduce( (prev, curr,i) =>  new_array[i] = prev + curr , 0 )
    return (new_array);

but I never manage to let it happen in the original array of objects or adding the thing to the original array of objects.

Thank you in advance!

1
  • I'm not sure putting the running sum inside the data array is the right approach; if anything reorders the array, the subtotals won't make any sense. Though I guess as long as the subtotals are in the order determined by the date field, you're fine... Commented Sep 22, 2022 at 15:44

3 Answers 3

2

If you want to mutate the original array don't create a new one (let new_array = [];), simply iterate over the original and add the property you want to each object.

const input = [{ date: '01-01-2022', count: 1 }, { date: '02-01-2022', count: 2 }, { date: '05-01-2022', count: 9 }]

let sum = 0;
for (const o of input) {
  o.sum = (sum += o.count);
}

console.log(input);

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

Comments

1

Here's something that would work for your case since you want to do it in original array of objects:

let myArray = [{date: '01-01-2022' , count: 1},
 {date: '02-01-2022' , count: 2},
 {date: '05-01-2022' , count: 9}];

myArray.map((entry, index, arr) => {
    entry.sum = index? arr[index-1].sum + entry.count : entry.count;
    delete entry.count; // Provides alternative outcome, remove for expected outcome
    return entry;
});

console.log(myArray);

5 Comments

You're using map() as a forEach here, just use forEach
I thought of using forEach. Where do I have to put it?
instead of the mapmyArray.forEach((entry, index, arr) => {...
@Hemant Singh Bisht map method for creating a new set of array you don't need to change the exiting array. if that is the case you can use for loops forEach just like @pilchard said
Map does modify the original array too which was the requirement. However, it additionally returns it too. I had learned somewhere that map is faster than forEach so I got into this BAD habit of using map if it can replace forEach. Thank you for the suggestions, though.
1

we can achieve by map also

   

 

const input = [{ date: '01-01-2022', count: 1 },{ date: '02-01-2022', count: 2 },{ date: '05-01-2022', count: 9 }]

const fun = (ar)=>{
  var t = 0
  const get = ar.map((e, i)=> {
    t = t+e.count
    const Data = {date : e.date,count : e.count,sum : t}
    return Data
})
  return get
}
console.log(fun(input))

Comments

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.