1

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
}]

And the end result I am hoping to find :

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

I will appreciate your help a lot !

Thanks in advance Q.

3 Answers 3

1

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

0
0

Group them based on HashTag and then sum the attributes

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

0

Here is a simplified snipped using 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;
}, []);

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.