2017-03-01 21 views
0

JSとJSONを初めて使用しています。私は、私は2つのP1値を持っている私は、重複する値を削除し、Q4のためのような最大の重みで単一の値のみを残しておきたいこの値の重さに基づいてJSONオブジェクトから重複値を削除します

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

のようなJSONオブジェクトを持っています。 /:しかし、私はちょうど私がこの

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

あなたは[Googleで検索]にしてみてくださいました(HTTPSのようなオブジェクトが欲しい106

で最大の重みとここ1が必要/www.google.com/search?q=Remove+duplicate+value+from+JSON+object)をタイトルに追加しますか? – mplungjan

+0

名前をキーとして使用するオブジェクトを作成します。値はウェイトです。元の配列をループし、現在の要素の重みがオブジェクトの対応する要素の重みよりも大きいかどうかをテストします。 – Barmar

答えて

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

関連する問題