0
点赞
收藏
分享

微信扫一扫

js数组对象中获取不重复项-简洁版

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>

</body>


<script>

let arr = [{
name: "张三",
id: "1"
},
{
name: "李四",
id: "2"
},
{
name: "李四",
id: "2"
},
{
name: "小兰",
id: "3"
},
{
name: "哈哈",
id: "3"
},
{
name: "张三",
id: "1"
},
{
name: "张三",
id: "1"
}
]

function fun(arr) {
let newArr = [...arr];
let indexArr = [];
arr.forEach((s, i) => {
let count = 0;
newArr.forEach((m, j) => {
if (arr[i].name == newArr[j].name) {
count++;
}
})
if (count === 1) {
indexArr.push(s);
}
})
return indexArr
}
let list = fun(arr);
console.log(list, "结果")

</script>

</html>

举报

相关推荐

0 条评论