我有一个包含对象的数组。我需要检查每个具有特定键值的对象,并删除具有相同值的对象..
var data = [
{
"XPOS":0,
"VALUE":0.14,
"USERID":"46473",
"MORE_USER_COUNT":1
},
{
"XPOS":1,
"VALUE":0.39,
"USERID":"46477",
"MORE_USER_COUNT":2
},
{
"XPOS":1,
"VALUE":0.39,
"USERID":"46475",
"MORE_USER_COUNT":2
},
{
"XPOS":1,
"VALUE":0.39,
"USERID":"46479",
"MORE_USER_COUNT":2
},
{
"XPOS":0,
"VALUE":0.94,
"USERID":"46471",
"MORE_USER_COUNT":1
},
{
"XPOS":0,
"VALUE":2.42,
"USERID":"46474",
"MORE_USER_COUNT":1
},
{
"XPOS":0,
"VALUE":4.78,
"USERID":"46472",
"MORE_USER_COUNT":1
}
]
我需要以下格式
var outdata = [
{
"XPOS":0,
"VALUE":0.14,
"USERID":"46473",
"MORE_USER_COUNT":1
},
{
"XPOS":1,
"VALUE":0.39,
"USERID":"46477",
"MORE_USER_COUNT":2
},
{
"XPOS":0,
"VALUE":0.94,
"USERID":"46471",
"MORE_USER_COUNT":1
},
{
"XPOS":0,
"VALUE":2.42,
"USERID":"46474",
"MORE_USER_COUNT":1
},
{
"XPOS":0,
"VALUE":4.78,
"USERID":"46472",
"MORE_USER_COUNT":1
}
]
答案 0 :(得分:1)
var arrData = [
{
"XPOS": 0,
"VALUE": 0.14,
"USERID": "46473",
"MORE_USER_COUNT": 1
},
{
"XPOS": 1,
"VALUE": 0.39,
"USERID": "46477",
"MORE_USER_COUNT": 2
},
{
"XPOS": 1,
"VALUE": 0.39,
"USERID": "46475",
"MORE_USER_COUNT": 2
},
{
"XPOS": 1,
"VALUE": 0.39,
"USERID": "46479",
"MORE_USER_COUNT": 2
},
{
"XPOS": 0,
"VALUE": 0.94,
"USERID": "46471",
"MORE_USER_COUNT": 1
},
{
"XPOS": 0,
"VALUE": 2.42,
"USERID": "46474",
"MORE_USER_COUNT": 1
},
{
"XPOS": 0,
"VALUE": 4.78,
"USERID": "46472",
"MORE_USER_COUNT": 1
}
];
document.getElementById("real_data").appendChild(document.createTextNode(JSON.stringify(arrData, null, "\t")));
// FUNCTION TO REMOVE DUPLICATE ELEMENT BASED ON KEY
function removeDuplicate() {
arrData.forEach(function (objElement1, intIndex1) {
arrData.forEach(function (objElement2, intIndex2) {
if (intIndex1 !== intIndex2 && objElement1.XPOS === objElement2.XPOS && objElement1.VALUE === objElement2.VALUE)
{
// REMOVE ARRAY ELEMENT
arrData.splice(intIndex2, 1);
}
});
});
}
removeDuplicate() ;
document.getElementById("expected_data").innerHTML = "";
document.getElementById("expected_data").appendChild(document.createTextNode(JSON.stringify(arrData, null, 4)));

#real_data,
#expected_data {
white-space: pre;
font-family: monospace;
font-size: 12px;
}

<body>
<div id="real_data"></div>
<hr/>
<div id="expected_data"></div>
</body>
&#13;
答案 1 :(得分:1)
您似乎正在尝试从数据数组中删除具有重复&#34; VALUE&#34;的对象。值。如果是这种情况,您应该能够创建临时数组,并且如果临时数组中已经存在匹配,则将对象从原始数组推送到临时数组。
问题在于找出哪个具有您想要保留的重复值的对象。目前尚不清楚标准是什么。
var original_values = [{
"XPOS": 0,
"VALUE": 0.14,
"USERID": "46473",
"MORE_USER_COUNT": 1
}, {
"XPOS": 1,
"VALUE": 0.39,
"USERID": "46477",
"MORE_USER_COUNT": 2
}, {
"XPOS": 1,
"VALUE": 0.39,
"USERID": "46475",
"MORE_USER_COUNT": 2
}, {
"XPOS": 1,
"VALUE": 0.39,
"USERID": "46479",
"MORE_USER_COUNT": 2
}, {
"XPOS": 0,
"VALUE": 0.94,
"USERID": "46471",
"MORE_USER_COUNT": 1
}, {
"XPOS": 0,
"VALUE": 2.42,
"USERID": "46474",
"MORE_USER_COUNT": 1
}, {
"XPOS": 0,
"VALUE": 4.78,
"USERID": "46472",
"MORE_USER_COUNT": 1
}];
var pruned_values = [];
function pruneArray(array, searchKey) {
for (var i = 0; i < array.length; i++) {
var theValue = array[i][searchKey];
if (!checkForDuplicates(pruned_values, searchKey, theValue)) {
pruned_values.push(array[i]);
}
}
original_values = pruned_values;
}
function checkForDuplicates(array, searchKey, searchValue) {
var foundDuplicate = false;
for (var i = 0; i < array.length; i++) {
if (array[i][searchKey] === searchValue) {
foundDuplicate = true;
}
}
return foundDuplicate;
}
pruneArray(original_values, "VALUE");