2017-06-14 176 views
1

我有JSON數組這樣,如何從JSON數組中刪除重複的對象

daysdif = [{ 
    "EmployeeID": "213654654", 
    "DaysDiff": "NaN" 
}, { 
    "EmployeeID": "String", 
    "DaysDiff": "NaN" 
}, { 
    "EmployeeID": "6021240", 
    "DaysDiff": "-63.30" 
}, { 
    "EmployeeID": "6011327", 
    "DaysDiff": "-35.67" 
}, { 
    "EmployeeID": "883", 
    "DaysDiff": "-63.40" 
}, { 
    "EmployeeID": "1183", 
    "DaysDiff": "-70.13" 
}, { 
    "EmployeeID": "1240", 
    "DaysDiff": "-70.97" 
}, { 
    "EmployeeID": "2293", 
    "DaysDiff": "-63.30" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-8.47" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-22.20" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-23.77" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-41.67" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-63.97" 
}, { 
    "EmployeeID": "3395", 
    "DaysDiff": "-68.00" 
}, { 
    "EmployeeID": "2473", 
    "DaysDiff": "-66.20" 
}, { 
    "EmployeeID": "1075", 
    "DaysDiff": "-70.17" 
}, { 
    "EmployeeID": "2947", 
    "DaysDiff": "-69.10" 
}, { 
    "EmployeeID": "5002196", 
    "DaysDiff": "-4.97" 
}, { 
    "EmployeeID": "5002196", 
    "DaysDiff": "-39.23" 
}, { 
    "EmployeeID": "5002196", 
    "DaysDiff": "-58.73" 
}, { 
    "EmployeeID": "1688", 
    "DaysDiff": "-67.40" 
}, { 
    "EmployeeID": "2031", 
    "DaysDiff": "-54.07" 
}, { 
    "EmployeeID": "1484", 
    "DaysDiff": "-69.17" 
}, { 
    "EmployeeID": "1022", 
    "DaysDiff": "-69.40" 
}] 

有這JSON數組一樣重複的值,

[{ 
    "EmployeeID": "5001839", 
    "DaysDiff": "-8.47" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-22.20" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-23.77" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-41.67" 
}, { 
    "EmployeeID": "5001839", 
    "DaysDiff": "-63.97" 
}] 

我需要保持最高DaysDiff值(在這裏-8.47)並刪除其他重複。

+0

你嘗試過什麼? – Rajesh

+0

是的,我可以刪除重複的EmployeeID,但我需要保持最高的DaysDiff值。 – Hashan

+0

在上面的提及中,@aefxx只是取代了最後一個值。相反,檢查對象是否具有該鍵,如果是,則分配'Math.max(ObjectValue,CurrentValue)' – Rajesh

回答

1

我這裏怎麼實現的使用underscore.js

json_data = [ 
 
    { 
 
    "EmployeeID": "213654654", 
 
    "DaysDiff": "NaN" 
 
    }, 
 
    { 
 
    "EmployeeID": "String", 
 
    "DaysDiff": "NaN" 
 
    }, 
 
    { 
 
    "EmployeeID": "6021240", 
 
    "DaysDiff": "-63.30" 
 
    }, 
 
    { 
 
    "EmployeeID": "6011327", 
 
    "DaysDiff": "-35.67" 
 
    }, 
 
    { 
 
    "EmployeeID": "883", 
 
    "DaysDiff": "-63.40" 
 
    }, 
 
    { 
 
    "EmployeeID": "1183", 
 
    "DaysDiff": "-70.13" 
 
    }, 
 
    { 
 
    "EmployeeID": "1240", 
 
    "DaysDiff": "-70.97" 
 
    }, 
 
    { 
 
    "EmployeeID": "2293", 
 
    "DaysDiff": "-63.30" 
 
    }, 
 
    { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-8.47" 
 
    }, 
 
    { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-22.20" 
 
    }, 
 
    { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-23.77" 
 
    }, 
 
    { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-41.67" 
 
    }, 
 
    { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-63.97" 
 
    }, 
 
    { 
 
    "EmployeeID": "3395", 
 
    "DaysDiff": "-68.00" 
 
    }, 
 
    { 
 
    "EmployeeID": "2473", 
 
    "DaysDiff": "-66.20" 
 
    }, 
 
    { 
 
    "EmployeeID": "1075", 
 
    "DaysDiff": "-70.17" 
 
    }, 
 
    { 
 
    "EmployeeID": "2947", 
 
    "DaysDiff": "-69.10" 
 
    }, 
 
    { 
 
    "EmployeeID": "5002196", 
 
    "DaysDiff": "-4.97" 
 
    }, 
 
    { 
 
    "EmployeeID": "5002196", 
 
    "DaysDiff": "-39.23" 
 
    }, 
 
    { 
 
    "EmployeeID": "5002196", 
 
    "DaysDiff": "-58.73" 
 
    }, 
 
    { 
 
    "EmployeeID": "1688", 
 
    "DaysDiff": "-67.40" 
 
    }, 
 
    { 
 
    "EmployeeID": "2031", 
 
    "DaysDiff": "-54.07" 
 
    }, 
 
    { 
 
    "EmployeeID": "1484", 
 
    "DaysDiff": "-69.17" 
 
    }, 
 
    { 
 
    "EmployeeID": "1022", 
 
    "DaysDiff": "-69.40" 
 
    } 
 
] 
 

 
grp_json = _.groupBy(json_data, "EmployeeID") 
 

 
new_list = [] 
 

 
_.each(grp_json, function(i) { 
 
    get_max = _.max(i, "DaysDiff") 
 
    if(get_max == -Infinity) { 
 
    new_list.push(i[0])  
 
    } 
 
    else { 
 
    new_list.push(get_max) 
 
    } 
 
}) 
 

 
console.log('new_list', new_list)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

0

請試試這個。

var daysdif = [{ 
 
    "EmployeeID": "213654654", 
 
    "DaysDiff": "NaN" 
 
}, { 
 
    "EmployeeID": "String", 
 
    "DaysDiff": "NaN" 
 
}, { 
 
    "EmployeeID": "6021240", 
 
    "DaysDiff": "-63.30" 
 
}, { 
 
    "EmployeeID": "6011327", 
 
    "DaysDiff": "-35.67" 
 
}, { 
 
    "EmployeeID": "883", 
 
    "DaysDiff": "-63.40" 
 
}, { 
 
    "EmployeeID": "1183", 
 
    "DaysDiff": "-70.13" 
 
}, { 
 
    "EmployeeID": "1240", 
 
    "DaysDiff": "-70.97" 
 
}, { 
 
    "EmployeeID": "2293", 
 
    "DaysDiff": "-63.30" 
 
}, { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-8.47" 
 
}, { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-22.20" 
 
}, { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-23.77" 
 
}, { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-41.67" 
 
}, { 
 
    "EmployeeID": "5001839", 
 
    "DaysDiff": "-63.97" 
 
}, { 
 
    "EmployeeID": "3395", 
 
    "DaysDiff": "-68.00" 
 
}, { 
 
    "EmployeeID": "2473", 
 
    "DaysDiff": "-66.20" 
 
}, { 
 
    "EmployeeID": "1075", 
 
    "DaysDiff": "-70.17" 
 
}, { 
 
    "EmployeeID": "2947", 
 
    "DaysDiff": "-69.10" 
 
}, { 
 
    "EmployeeID": "5002196", 
 
    "DaysDiff": "-4.97" 
 
}, { 
 
    "EmployeeID": "5002196", 
 
    "DaysDiff": "-39.23" 
 
}, { 
 
    "EmployeeID": "5002196", 
 
    "DaysDiff": "-58.73" 
 
}, { 
 
    "EmployeeID": "1688", 
 
    "DaysDiff": "-67.40" 
 
}, { 
 
    "EmployeeID": "2031", 
 
    "DaysDiff": "-54.07" 
 
}, { 
 
    "EmployeeID": "1484", 
 
    "DaysDiff": "-69.17" 
 
}, { 
 
    "EmployeeID": "1022", 
 
    "DaysDiff": "-69.40" 
 
}]; 
 

 
var newArray = []; 
 
var isExist = true;; 
 

 
daysdif.forEach(function(data){ 
 
    isExist = true; 
 
    if(data["DaysDiff"] !== "NaN"){ 
 
    newArray.forEach(function(obj){ 
 
     if(obj["EmployeeID"] == data["EmployeeID"]){ 
 
      newArray["DaysDiff"] = Math.max(parseFloat(data["DaysDiff"]), parseFloat(newArray["DaysDiff"])).toString(); 
 
      isExist = false; 
 
     } 
 
    }); 
 
     if(isExist){ 
 
      newArray.push(data); 
 
     } 
 
    } 
 
}); 
 
console.log(newArray);

0

試試這個....

var uniqueEmp=[]; 
for(var emp in daysdif){ 
    if(uniqueEmp.indexOf(daysdif[emp]['EmployeeID'])!=-1){ 
     daysdif.splice(emp,1); 
    }else 
     uniqueEmp.push(daysdif[emp]['EmployeeID']) 
} 
console.log(daysdif) 
相關問題