怎么删除 这个json数组中相同的数据 只要相同两条数据都删除
把两个{gx: "粗扯", pgbh: "202201080001"} 都删除点 只剩下 单一的数据
Json数据如何删除相同的两个相同的元素!
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注 function removeDuplicates(arr) { const result = []; const duplicatesIndices = []; // Loop through each item in the original array arr.forEach((current, index) => { if (duplicatesIndices.includes(index)) return; result.push(current); // Loop through each other item on array after the current one for (let comparisonIndex = index + 1; comparisonIndex < arr.length; comparisonIndex++) { const comparison = arr[comparisonIndex]; const currentKeys = Object.keys(current); const comparisonKeys = Object.keys(comparison); // Check number of keys in objects if (currentKeys.length !== comparisonKeys.length) continue; // Check key names const currentKeysString = currentKeys.sort().join("").toLowerCase(); const comparisonKeysString = comparisonKeys.sort().join("").toLowerCase(); if (currentKeysString !== comparisonKeysString) continue; // Check values let valuesEqual = true; for (let i = 0; i < currentKeys.length; i++) { const key = currentKeys[i]; if ( current[key] !== comparison[key] ) { valuesEqual = false; break; } } if (valuesEqual) duplicatesIndices.push(comparisonIndex); } // end for loop }); // end arr.forEach() return result; } const books = [ { name: "My Sister the Serial Killer", author: "Oyinkan Braithwaite" }, { name: "Educated", author: "Tara Westover" }, { name: "My Sister the Serial Killer", author: "Oyinkan Braithwaite" } ]; console.log(removeDuplicates(books)) // [ // { // name: 'My Sister the Serial Killer', // author: 'Oyinkan Braithwaite' // }, // { name: 'Educated', author: 'Tara Westover' } // ]
解决 无用评论 打赏 举报
悬赏问题
- ¥15 phython路径名过长报错 不知道什么问题
- ¥15 深度学习中模型转换该怎么实现
- ¥15 HLs设计手写数字识别程序编译通不过
- ¥15 Stata外部命令安装问题求帮助!
- ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
- ¥15 TYPCE母转母,插入认方向
- ¥15 如何用python向钉钉机器人发送可以放大的图片?
- ¥15 matlab(相关搜索:紧聚焦)
- ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
- ¥15 Arduino无法同时连接多个hx711模块,如何解决?