Harsh sachdev Harsh sachdev -4 years ago 160
Javascript Question

Remove duplicate value from JSON object based upon weight of value

I am new to JS and JSON. I have a JSON object like this

{"q1":[{"name":"p1","weight":64},{"name":"p3","weight":56}],"q2":[{"name":"p1","weight":56},{"name":"p2","weight":56},{"name":"p4","weight":56},{"name":"p5","weight":56},{"name":"p6","weight":64}],"q3":[{"name":"p1","weight":48},{"name":"p2","weight":64},{"name":"p3","weight":64}],"q4":[{"name":"p1","weight":64},{"name":"p1","weight":106},{"name":"p2","weight":56},{"name":"p3","weight":56},{"name":"p3","weight":112}],"q5":[{"name":"p1","weight":64},{"name":"p1","weight":113},{"name":"p2","weight":49},{"name":"p3","weight":56},{"name":"p3","weight":56},{"name":"p4","weight":49},{"name":"p5","weight":49},{"name":"p6","weight":56}],"q6":[]}


I want to remove duplicate value and keep only single value with the largest weight like for Q4 I have two P1 value. But I just need 1 here with the largest weight that is 106

I want an Object like this

{"q1":[{"name":"p1","weight":64},{"name":"p3","weight":56}],"q2":[{"name":"p1","weight":56},{"name":"p2","weight":56},{"name":"p4","weight":56},{"name":"p5","weight":56},{"name":"p6","weight":64}],"q3":[{"name":"p1","weight":48},{"name":"p2","weight":64},{"name":"p3","weight":64}],"q4":[{"name":"p1","weight":106},{"name":"p2","weight":56},{"name":"p3","weight":112}],"q5":[{"name":"p1","weight":113},{"name":"p2","weight":49},{"name":"p3","weight":56},{"name":"p4","weight":49},{"name":"p5","weight":49},{"name":"p6","weight":56}],"q6":[]}

Answer Source

function keepBiggest(arr) {                 // take an array of object and return unique values that have the biggest weight
  var hash = {};                            // the hash object the hash the max values
  arr.forEach(function(o) {                 // for each object o in the array arr
    if(hash[o.name]) {                      // if we already hashed this an object with the same name
      if(hash[o.name].weight < o.weight)    // if the hashed object is lighter than this object
        hash[o.name] = o;                   // then override the hashed object with this one
    }
    else                                    // if we haven't hashed this name yet
      hash[o.name] = o;                     // then assume that this object is the heaviest of its kind
  });

  return Object.keys(hash).map(function(key) { // transform the hash object into an array and return it
    return hash[key];
  });
}

function removeDuplicates(obj) {               // take an object that contains arrays and for each array leave only the heaviest objects
  Object.keys(obj).forEach(function(key) {     // for each key in the object
    obj[key] = keepBiggest(obj[key]);          // remove duplicates from the array at that key
  });
}

var object = {"q1":[{"name":"p1","weight":64},{"name":"p3","weight":56}],"q2":[{"name":"p1","weight":56},{"name":"p2","weight":56},{"name":"p4","weight":56},{"name":"p5","weight":56},{"name":"p6","weight":64}],"q3":[{"name":"p1","weight":48},{"name":"p2","weight":64},{"name":"p3","weight":64}],"q4":[{"name":"p1","weight":64},{"name":"p1","weight":106},{"name":"p2","weight":56},{"name":"p3","weight":56},{"name":"p3","weight":112}],"q5":[{"name":"p1","weight":64},{"name":"p1","weight":113},{"name":"p2","weight":49},{"name":"p3","weight":56},{"name":"p3","weight":56},{"name":"p4","weight":49},{"name":"p5","weight":49},{"name":"p6","weight":56}],"q6":[]};

removeDuplicates(object);
console.log(object);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download