最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Merge objects with same key in javascript - Stack Overflow

programmeradmin2浏览0评论

I have been trying to figure this out for a long time, if i have an array of objects like so:

var my_array = [
    Object {Project: A, Hours: 2},
    Object {Project: B, Hours: 3},
    Object {Project: C, Hours: 5},
    Object {Project: A, Hours: 6},
    Object {Project: C, Hours: 9}
]

I want to merge all the objects with the same key together into one object, such that their hours get added up:

Expected output:

my_array = [
    Object {Project: A, Hours: 8}
    Object {Project: B, Hours: 3}
    Object {Project: C, Hours: 14}
]

How can I approach this issue? It has taken me a long time to get my data formatted in this way, this is the last step!

My attempt, I get that I am looping through array, not sure how to deal with the merging of the objects:

for (var i =0; i<my_array.length; i++) {
   my_array[i].Project   // access the object project key
   my_array[i].Hours     // need to increment hours
}

I have been trying to figure this out for a long time, if i have an array of objects like so:

var my_array = [
    Object {Project: A, Hours: 2},
    Object {Project: B, Hours: 3},
    Object {Project: C, Hours: 5},
    Object {Project: A, Hours: 6},
    Object {Project: C, Hours: 9}
]

I want to merge all the objects with the same key together into one object, such that their hours get added up:

Expected output:

my_array = [
    Object {Project: A, Hours: 8}
    Object {Project: B, Hours: 3}
    Object {Project: C, Hours: 14}
]

How can I approach this issue? It has taken me a long time to get my data formatted in this way, this is the last step!

My attempt, I get that I am looping through array, not sure how to deal with the merging of the objects:

for (var i =0; i<my_array.length; i++) {
   my_array[i].Project   // access the object project key
   my_array[i].Hours     // need to increment hours
}
Share Improve this question edited Mar 22, 2016 at 6:18 thefourtheye 240k53 gold badges465 silver badges500 bronze badges asked Mar 22, 2016 at 6:10 TABISH KHANTABISH KHAN 1,6231 gold badge20 silver badges33 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You create another object where you can actually group the projects and accumulate the corresponding hours, like this

var groups = my_array.reduce(function(resultObject, currentObject) {

    // if this is the first time the project appears in the array, use zero as the
    // default hours
    resultObject[currentObject.Project] = resultObject[currentObject.Project] || 0;

    // add the current hours corresponding to the project
    resultObject[currentObject.Project] += currentObject.Hours;

    return resultObject;
}, {});

At this point, your groups will look like this

console.log(groups);
// { A: 8, B: 3, C: 14 }

Now, you just have to expand this object, like this

var result = Object.keys(groups).map(function(currentGroup) {
    return {Project: currentGroup, Hours: groups[currentGroup]};
});

Now, the result will be

[ { Project: 'A', Hours: 8 },
  { Project: 'B', Hours: 3 },
  { Project: 'C', Hours: 14 } ]

In your attempt, you are missing out on creating a new array

var newArray = [];
var uniqueprojects = {};
for (var i =0; i<my_array.length; i++) {

   if ( !uniqueproject[my_array[i].Project] )
   {
     uniqueproject[my_array[i].Project] = 0;
   }
   uniqueproject[my_array[i].Project] += my_array[i].Hours;
   //my_array[i].Project   // access the object project key
   //my_array[i].Hours     // need to increment hours
}

Now create the final output array out of uniqueproject map

newArray = Object.keys(uniqueproject).map(function(key){return {Project:key, Hours:uniqueproject[key]}});

Both thefourtheye's and gurvinder372's proposed solutions work, so I set up a benchmark test on jsPerf to test which is faster. You can see it here.

It appears that gurvinder372's code is by far the fastest.

P.S. please ignore the Uncaught TypeError as it is a jsPerf issue currently being fixed that has nothing to do with the results of the test. For more info, see this and this.

发布评论

评论列表(0)

  1. 暂无评论