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" }, { "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>