2017-03-01 127 views
0

我是JS和JSON的新手。我有這樣基於值的權重從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對象我有兩個P1值。但我只需要1這裏是106

我想這樣

{"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

你嘗試到對象的最大重量[搜索谷歌(HTTPS:/ /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);