razorsyntax
razorsyntax

Reputation: 359

Combining Like Objects by Date in JavaScript Array

I have the following array:

var objArray = [
    { num: 1, date: '1/12/2017' },
    { num: 3, date: '1/12/2017' },
    { num: 7, date: '1/12/2017' },
    { num: 1, date: '1/13/2018' },
    { num: 3, date: '1/16/2018' },
    { num: 4, date: '1/16/2018' }
   ];

I want to combine those with same dates so that the output array looks like this:

var outputArr = [
    { num: 11, date: '1/12/2017' },
    { num: 1,  date: '1/13/2018' },
    { num: 7,  date: '1/16/2018' }
   ];

I'm adding all num with similar dates and creating a single new object.

I have a very large dataset of objects like this so I'm trying to reduce the amount of processing time for this.

I've got the arrays sorted by date so that it mirrors objArray.

For loops seems cumbersome since I'm taking the first date in the array and checking every other element in the array a la the following pseudo-code:

var newArr = [];
for(i = 0; i < objArray.length; i++) {
    for(j = 0; j < objArray.length; j++) {
        var tempArr = [];
        // check every date manually
        // add similar to new array
        tempArr.push({ similar items });
    }
    newArr.push(tempArr):
}

// Do another couple loops to combine those like arrays into another array    

There has to be a more elegant way to perform this than running multiple for loops.

Any suggestions would be appreciated.

Upvotes: 2

Views: 1510

Answers (5)

vaconingham
vaconingham

Reputation: 21

Here's another way. It's more verbose, but if you're just starting out it might be easier to understand as opposed to using array methods like reduce().

objArray = [
    { num: 1, date: '1/12/2017' },
    { num: 3, date: '1/12/2017' },
    { num: 7, date: '1/12/2017' },
    { num: 1, date: '1/13/2018' },
    { num: 3, date: '1/16/2018' },
    { num: 4, date: '1/16/2018' }
]


function combineObj(data) {
    let validator= new Set();
    let combinedArr = [];
    let numCount = 0;
    
    // Create a list of unique properties to match against:
    data.forEach((e) => validator.add(e.date));

    // For each value in the validator, create a new object in a new array 
    // and add the unique values from the validator to the respective property:
    validator.forEach((e) => {
        combinedArr.push({
            num: 0,
            date: e
        });
    })

    // Lastly, for each object in the combinedArr, use a counter to sum up the total values of each property
    // as you loop through your data:
    combinedArr.forEach((e) => {
        numCount = 0;
        data.forEach((ee) => {
            if (e.date === ee.date) {
                numCount += ee.num;
                e.num = numCount;
            }
        })
    })

    return combinedArr;
}

Returns:

[
  { num: 11, date: '1/12/2017' },
  { num: 1, date: '1/13/2018' },
  { num: 7, date: '1/16/2018' }
]

Upvotes: 0

Erick
Erick

Reputation: 1246

You could also remove the if statements and use a Set if you wanted to be even more declarative.

var objArray = [
    { num: 1, date: '1/12/2017' },
    { num: 3, date: '1/12/2017' },
    { num: 7, date: '1/12/2017' },
    { num: 1, date: '1/13/2018' },
    { num: 3, date: '1/16/2018' },
    { num: 4, date: '1/16/2018' }
];
var mSet = new Set(objArray.map(d => d.date));
return Array.from(mSet).map(d => {
    return
    { 
        date: d,
        sum: (objArray
            .filter(o => o.date === d)
            .map(n => n.num)
            .reduce((a, c) => a + c, 0))
    }
);

This returns:

[{ date: 1/12/2017, sum: 11},
{ date: 1/13/2018, sum: 1 },
{ date: 1/16/2018, sum: 7 }]

Upvotes: 1

Hero Qu
Hero Qu

Reputation: 951

var objArray = [
    { num: 1, date: '1/12/2017' },
    { num: 3, date: '1/12/2017' },
    { num: 7, date: '1/12/2017' },
    { num: 1, date: '1/13/2018' },
    { num: 3, date: '1/16/2018' },
    { num: 4, date: '1/16/2018' }
   ];

let outputArr = Array.from(objArray.reduce((acc, obj)=>{
  acc.set(obj.date, (acc.get([obj.date]) || 0) + obj.num);
  return acc;
}, new Map()))
.map(kv=>({num: kv[1], date: kv[0]}))

console.log(outputArr);

gives:

[ { num: 11, date: '1/12/2017' },
  { num: 1, date: '1/13/2018' },
  { num: 7, date: '1/16/2018' } ]

Upvotes: 1

Cody Geisler
Cody Geisler

Reputation: 8617

Using lodash,

// Aggregate num from unique dates
var g = _.groupBy(objArray,'date')
Object.keys(g).map(k=>({num:g[k].reduce((a,c)=>c.num+a,0),date:k})) 

Upvotes: 2

amrender singh
amrender singh

Reputation: 8239

Simply use Array.reduce() to create a map and group values by date, Object.values() on the map will give you the desired output value:

let arr = [ { num: 1, date: '1/12/2017' }, { num: 3, date: '1/12/2017' }, { num: 7, date: '1/12/2017' }, { num: 1, date: '1/13/2018' }, { num: 3, date: '1/16/2018' }, { num: 4, date: '1/16/2018' } ];
   
let result = Object.values(arr.reduce((a, {num, date})=>{
  if(!a[date])
    a[date] = Object.assign({},{num, date});
   else
    a[date].num += num;
  return a;
 },{}));
 console.log(result);

Upvotes: 5

Related Questions