JS两个对象数组合并并去重JS两个对象数组合并并去重
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
</html>
<script type="text/javascript">
let jsonArr = [
{
"ID": "",
"NO": "1",
"NAME": "测试对象1"
},
{
"ID": "",
"NO": "2",
"NAME": "测试对象2"
},
{
"ID": "",
"NO": "3",
"NAME": "测试对象3"
},
{
js合并两个数组"ID": "",
"NO": "4",
"NAME": "测试对象4"
},
{
"ID": "",
"NO": "5",
"NAME": "测试对象5"
}
];
let jsonArr2 = [
{
"ID": "",
"NO": "3",
"NAME": "测试对象3"
},
{
"ID": "",
"NO": "4",
"NAME": "测试对象4"
},
{
"ID": "",
"NO": "6",
"NAME": "测试对象6"
},
{
"ID": "",
"NO": "7",
"NAME": "测试对象7"
}
];
let length1 = jsonArr.length;
let length2 = jsonArr2.length;
for (let i = 0; i < length1; i++) {
for (let j = 0; j < length2; j++)
{
//判断添加的数组是否为空了
if (jsonArr.length > 0) {
if (jsonArr[i]["NAME"] === jsonArr2[j]["NAME"]) {
jsonArr.splice(i, 1); //利⽤splice函数删除元素,从第i个位置,截取长度为1的元素 length1--;
console.log(jsonArr2[j]);//重复元素
}
}
}
}
for (let n = 0; n < jsonArr2.length; n++) {
jsonArr.push(jsonArr2[n]);
}
console.log(JSON.stringify(jsonArr));
</script>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论