0

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":[]}
Harsh sachdev
  • 217
  • 1
  • 2
  • 13
  • Create an object that uses the name as the key, and the value is the weight. Loop through the original array, testing whether the weight of the current element is more than the weight in the corresponding element of the object. – Barmar Mar 01 '17 at 17:30

1 Answers1

1

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);
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73