<!DOCTYPE html>
<
html>
<
head
lang=
"en">
<
meta
charset=
"UTF-8">
<
title></
title>
</
head>
<
body>
<
script>
var a
= [{ x:
10, y:
20, z:
30 }, { x:
10, y:
20, z:
30 }, { x:
50, y:
60, z:
60 }, { x:
10, y:
20, z:
30 }, { x:
20, y:
20, z:
30 }];
b
=
unique(a);
//数组对象去重
console.
log(b);
//判断两对象是否相等
function
unique(
arr) {
var res
= [arr[
0]];
for (
var i
=
0; i
< arr.length; i
++) {
var repeat
=
false;
for (
let j
=
0; j
< res.length; j
++) {
if (
isObjectValueEqual(arr[i], res[j])) {
repeat
=
true;
break;
}
}
if (
!repeat) {
res.
push(arr[i]);
}
}
return res;
}
function
isObjectValueEqual(
a,
b) {
var aProps
=
Object.
getOwnPropertyNames(a);
var bProps
=
Object.
getOwnPropertyNames(b);
if (aProps.length
!= bProps.length) {
return
false;
}
for (
var i
=
0; i
< aProps.length; i
++) {
var propName
= aProps[i];
if (a[propName]
!== b[propName]) {
return
false;
}
}
return
true;
}
</
script>
</
body>
</
html>