如何基于属性合并数组中的对象

时间:2015-07-06 15:16:43

标签: javascript angularjs object merge

我正在尝试合并一个数组中的重复对象,同时添加一些属性值并将属性连接到一个数组中,但是我被卡住了。

这是我正在处理的数组类型:

[{
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "19h30",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 615850479952785400
}, {
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "80s",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 617319253738393600
}, {
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "80s",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 616521677275533300
}, {
   "Favorites": 1,
   "Frequency": 1,
   "Hashtag": "AloeBlacc",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 617309488572420100
}, {
   "Favorites": 2,
   "Frequency": 1,
   "Hashtag": "Alpes",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 615481266348146700
}]

我希望找到最终结果:

[{
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "19h30",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 615850479952785400
}{
   "Favorites": 0,
   "Frequency": 2,
   "Hashtag": "80s",
   "Replies": 0,
   "Retweets": 2,
   "tweetId": [617319253738393600 ,
               616521677275533300]
}, {
   "Favorites": 1,
   "Frequency": 0,
   "Hashtag": "AloeBlacc",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 617309488572420100
}, {
   "Favorites": 2,
   "Frequency": 0,
   "Hashtag": "Alpes",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 615481266348146700
}]

目前,我尝试在循环中循环,但没有成功,我被卡住了。

var mergedHashtags = [];
var merged={};
for (var i = 0; i < hashtags.length-1; i++) {
    var a = hashtags[i];
    // start second loop at next element in array
    for (var j = i+1; j < hashtags.length; j++) {
       var b = hashtags[j];
       console.log('a',a,'b',b, 'i',i,'j',j);
       if (a.Hashtag===b.Hashtag) {
          var tIds = [a.tweetId];
          merged.Hashtag    =  a.Hashtag;
          merged.tweetId    =  tIds.concat(b.tweetId);
          merged.Favorites  =  a.Favorites+b.Favorites;
          merged.Retweets   =  a.Retweets+b.Retweets;
          merged.Replies    =  a.Replies+b.Replies;
          merged.Frequency  =  a.Frequency+b.Frequency;
          mergedHashtags.push(merged);
          console.log('same', merged);
          continue;
       }else{
          mergedHashtags.push(a);
          i=j-1;
          console.log('diff',a);
          break;
       }
    }
}

我将非常感谢你的帮助!

提前致谢 Q值。

3 个答案:

答案 0 :(得分:1)

您可以创建临时商店object并按Hashtag存储商品,这样您就不必再循环查找是否存在具有相同Hashtag的商品

&#13;
&#13;
var merge = function(list) {
  var result = [];
  var store = {};
  var addables = ['Favorites', 'Retweets', 'Replies', 'Frequency'];

  list.forEach(function(item) {
    // Check exist by hashtag.
    if (typeof store[item.Hashtag] === 'undefined') {
      // clone item, not alter origin list.
      store[item.Hashtag] = JSON.parse(JSON.stringify(item));
      result.push(store[item.Hashtag]);
    } else {
      var soruce = store[item.Hashtag];
      // Check if its already combined or not.
      if( Object.prototype.toString.call(soruce.tweetId) === '[object Array]') {
        soruce.tweetId.push(item.tweetId);
      } else {
        soruce.tweetId = [soruce.tweetId, item.tweetId];
      }
      // Add addable attributes.
      addables.forEach(function(key) {
        soruce[key] += item[key];
      });
    }
  });

  return result;
};

var list = [{
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "19h30",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 615850479952785400
}, {
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "80s",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 617319253738393600
}, {
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "80s",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 616521677275533300
}, {
   "Favorites": 1,
   "Frequency": 1,
   "Hashtag": "AloeBlacc",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 617309488572420100
}, {
   "Favorites": 2,
   "Frequency": 1,
   "Hashtag": "Alpes",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 615481266348146700
}];

console.log(merge(list));
&#13;
&#13;
&#13;

答案 1 :(得分:0)

根据HashTag对它们进行分组,然后对属性进行求和

&#13;
&#13;
var items = [{
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "19h30",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 615850479952785400
}, {
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "80s",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 617319253738393600
}, {
   "Favorites": 0,
   "Frequency": 1,
   "Hashtag": "80s",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 616521677275533300
}, {
   "Favorites": 1,
   "Frequency": 1,
   "Hashtag": "AloeBlacc",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 617309488572420100
}, {
   "Favorites": 2,
   "Frequency": 1,
   "Hashtag": "Alpes",
   "Replies": 0,
   "Retweets": 1,
   "tweetId": 615481266348146700
}];

var groupBy = {};

items.forEach(function(item){
    groupBy[item.Hashtag]= groupBy[item.Hashtag] || [];
    groupBy[item.Hashtag].push(item);
});

var newArray = [], newObj ;


for(var k in groupBy) {
   newObj = {};
   newObj.Favorites = 0;
   newObj.Frequency = 0;
   newObj.Hashtag = groupBy[k].Hashtag;
   newObj.Replies = 0;
   newObj.Retweets = 0;
   newObj.tweetId = [];
   groupBy[k].forEach(function(item){
       newObj.Favorites += item.Favorites;
       newObj.Frequency +=  item.Frequency;
       newObj.Replies +=  item.Replies;
       newObj.Retweets +=  item.Retweets;
       newObj.tweetId.push(item.tweetId);
   });
   newArray.push(newObj);
}

document.body.innerHTML = JSON.stringify(newArray);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

以下是使用Array.prototype.reduce

的简化剪辑
array.reduce(function (rst, item) {
   var found = false;
   rst.forEach(function (resultItem) { 
       if(resultItem.Hashtag == item.Hashtag) { 
           found = true;
           resultItem.Favorites += item.Favorites;
           resultItem.Frequency += item.Frequency;
           resultItem.Replies += item.Replies;
           resultItem.Retweets += item.Retweets;
           if(JSON.stringify(resultItem.tweetId.__proto__)!='[]') {
                resultItem.tweetId = [resultItem.tweetId];
           }
           resultItem.tweetId.push(item.tweetId);
       }
   });
   !found && rst.push(item);
   return rst;
}, []);