我正在从后端获取JSON数组中的重复值,我想删除重复值并仅在SAPUI5 ViewSettingsItem中显示唯一值。
请帮助我。
A B C
0 0 0 0
1 1 1 1
2 2 4 8
3 3 9 27
}
XML视图:
{
"d": {
"results": [{
"Status": "Completed",
"sId": "C1"
},
{
"Status": "Completed",
"sId": "C1"
},
{
"Status": "Started",
"sId": "S1"
},
{
"Status": "In Process",
"sId": "P1"
},{
"Status": "Completed",
"sId": "C1"
}]
}
答案 0 :(得分:0)
您可以使用reduce和Object
来跟踪先前添加的sId's
let obj = {"d": {"results": [{"Status": "Completed","sId": "C1"}, {"Status": "Completed","sId": "C1"},{"Status": "Started","sId": "S1"},{"Status": "In Process","sId": "P1"},{"Status": "Completed","sId": "C1"}]}}
let track = {}
let results = obj.d.results.reduce((op,inp)=>{
if(!track[inp.sId]){
op.push(inp)
track[inp.sId] = inp
}
return op
},[])
let op = {d:{results:results}}
console.log(op)
答案 1 :(得分:0)
您可以使用javascript(无需使用ES6规范)和jQuery实现此功能。
var obj = {"d": {"results": [{"Status": "Completed","sId": "C1"}, {"Status": "Completed","sId": "C1"},{"Status": "Started","sId": "S1"},{"Status": "In Process","sId": "P1"},{"Status": "Completed","sId": "C1"}]}}
var uniqueNames = [];
/******************************************************************************/
//check if el exist in array
function inArray (array,el) {
var find = false;
$.each(array,function(index,value) {
if (el.sId === value.sId ) {
find = true;
return false
}
});
return find;
}
/*************************************************************************/
// remove duplicate object from the array
$.each(obj.d.results,function(i, el) {
if (this.inArray(uniqueNames,el) === false) {
uniqueNames.push(el)
}
}.bind(this)),
console.log(uniqueNames);
这是一个jsBin代码段:https://jsbin.com/vaxocor/edit?html,js,output