如何基于属性合并数组中的对象 [英] How to merge objects within array based on attribute

查看:81
本文介绍了如何基于属性合并数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to merge recurring object within an array while adding some of their attributes value and concatenating on attribute into an array, but I am stuck.

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

Here's the kind of array I am processing :

[{
   "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
}]

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

For the moment, I tried looping within a loop but with no success and I am stuck.

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。

Thanks in advance Q.

推荐答案

您可以创建临时存储对象并按 Hashtag 存储项目,这样您就不必再循环查找是否存在具有相同标签的项目

You can create a temp store object and store items by its Hashtag, so you don't have to loop again to find if there exist the item with same Hashtag.

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

这篇关于如何基于属性合并数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆