日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

數(shù)組去重并找到所有重復(fù)的項(xiàng)的索引位置

作者:Mr.一 更新時(shí)間: 2022-04-22 編程語言
例如此例子。找到該數(shù)組里面對(duì)象擁有相同name的對(duì)象索引,以及值      
      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) => {   //把要比較的數(shù)據(jù)單獨(dú)抽離出來。方便看
        arr.push(s.name);
      });
      const res = [];
      arr.map((i, index) => {     // 這里是核心,通過雙層循環(huán)。比較有沒有重復(fù)的項(xiàng)
        arr.map((p, ind) => {
          if (p === i && ind !== index) {  //找到重復(fù)的項(xiàng),并排除掉自身的索引
            res.push(ind);
          }
        });
      });
      let result = Array.from(new Set(res));   //數(shù)組去重方法
      console.log(result); //[3, 4, 6, 0]     //這里就得到重復(fù)項(xiàng)的索引位置
      const finallys = result.map((l) => {
        return objs[l];
      });
      console.log(finallys); //?[{…}, {…}, {…}, {…}]   //這里得到重復(fù)的具體的所有項(xiàng)值

原文鏈接:https://blog.csdn.net/weixin_46016926/article/details/118739163

欄目分類
最近更新