網站首頁 編程語言 正文
例如此例子。找到該數組里面對象擁有相同name的對象索引,以及值
const objs = [
{ name: "1", age: 12 },
{ name: "2", age: 12 },
{ name: "3", age: 12 },
{ name: "1", age: 12 },
{ name: "1", age: 12 },
{ name: "7", age: 12 },
{ name: "1", age: 12 },
];
const arr = [];
objs.map((s) => { //把要比較的數據單獨抽離出來。方便看
arr.push(s.name);
});
const res = [];
arr.map((i, index) => { // 這里是核心,通過雙層循環。比較有沒有重復的項
arr.map((p, ind) => {
if (p === i && ind !== index) { //找到重復的項,并排除掉自身的索引
res.push(ind);
}
});
});
let result = Array.from(new Set(res)); //數組去重方法
console.log(result); //[3, 4, 6, 0] //這里就得到重復項的索引位置
const finallys = result.map((l) => {
return objs[l];
});
console.log(finallys); //?[{…}, {…}, {…}, {…}] //這里得到重復的具體的所有項值
原文鏈接:https://blog.csdn.net/weixin_46016926/article/details/118739163
相關推薦
- 2022-03-16 .NET?6中的dotnet?monitor講解_實用技巧
- 2022-10-23 C#中new操作符的工作機制_C#教程
- 2023-01-11 Selenium中的option使用示例_python
- 2022-06-30 opencvsharp瑕疵檢測的實現示例_C#教程
- 2022-12-28 Android?ViewPager2?+?Fragment?聯動效果的實現思路_Android
- 2022-05-11 多線程編程(下):線程同步&通信
- 2022-07-10 css中border屬性設置
- 2022-05-12 寶塔面板配置SSL證書
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支