我有这样的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 :(得分: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>
答案 1 :(得分: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);
答案 2 :(得分: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)