<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script>
function displayDate(){
var arr = [{ d: '2015-10-12',C:'Apple'}, { d: '2015-10-12',C:'Apple'}, { d: '2015-10-13',C :'Apple' }];
var find = false;
for (var i = 0; i < arr.length; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i].d == arr[j].d && arr[i].C==arr[j].C) { find = true; break;}
}
if (find) break;
}
alert(find)
}
</script>
</head>
<body>
<h1>判断list中的json对象是否重复</h1>
<button type="button" onclick="displayDate()">是否重复</button>
</body>
</html>
js 删除数组json中重复的元素或者对象
var arr = [
{'id': 1, 'name': lx},
{'id': 1, 'name': lx001},
{'id': 1, 'name': lx002},
{'id': 1, 'name': lx},
{'id': 3, 'name': lx},
];
deteleObject(arr);
function deteleObject(obj) {
var uniques=[];
var stringify = {};
for(var i=0; i<obj.length; i++) {
var keys = Object.keys(obj[i]);
keys.sort(function(a, b) {
return (Number(a) - Number(b));
});
var str='';
for(var j=0; j < keys.length; j ++) {
str += JSON.stringify(keys[j]);
str += JSON.stringify(obj[i][keys[j]]);
}
if(!stringify.hasOwnProperty(str)) {
uniques.push(obj[i]);
stringify[str]=true;
}
}
return uniques.length;
}
结果:
[
{'id': 1, 'name': lx},
{'id': 1, 'name': lx001},
{'id': 1, 'name': lx002},
{'id': 3, 'name': lx},
];
网友评论