數(shù)組去重

兩個(gè)數(shù)組除去id相同的

   const arr1 = [
        {id:1,name:'a'},
        {id:2,name:'abc'},
        {id:3,name:'abbb'}, 
        {id:4,name:'abxxx'}, 
        {id:5,name:'xyz'},
        {id:6,name:'abcdef'},
        {id:7,name:'abzzzz'}
      ];
    const arr2 = [
        {id:1,name:'a'},
        {id:5,name:'xyz'},
        {id:7,name:'abzzzz'}
      ];
  let result = arr1.filter(item1 => arr2.every(item2 => item2.id !== item1.id))
  console.log(result)

輸出結(jié)果

image.png

單個(gè)數(shù)組去重,形成新的數(shù)組

方法1

  let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3, undefined, undefined, null, null, Object, Object];
  let set = Array.from(new Set(array));
  console.log(set);

輸出結(jié)果

image.png

方法2

  let arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 0, 8, 6, 3, 4, 56, 2, undefined, undefined, null, null, Object, Object];
  let arr2 = arr.filter((x, index, self) => self.indexOf(x) === index)
  console.log(arr2)

輸出結(jié)果

image.png

數(shù)組內(nèi)去除相同id的對(duì)象
方法1:

let person = [
     {id: 0, name: "小明"},
     {id: 1, name: "小張"},
     {id: 2, name: "小李"},
     {id: 3, name: "小孫"},
     {id: 1, name: "小周"},
     {id: 2, name: "小陳"}, 
    
];

let obj = {};

let arr = person.reduce((cur,next) => {
    obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
    return cur;
},[]) //設(shè)置cur默認(rèn)類型為數(shù)組,并且初始值為空的數(shù)組
console.log(arr);

輸出結(jié)果

image.png

方法2:

let person = [
     {id: 0, name: "小明", testId: 1},
     {id: 1, name: "小張", testId: 2},
     {id: 2, name: "小李", testId: 3},
     {id: 3, name: "小孫", testId: 4},
     {id: 1, name: "小周", testId: 1},
     {id: 2, name: "小陳", testId: 1}, 
    
];

function filterSameIndex(arr, index) {
    // arr是數(shù)組,index是要過濾的目標(biāo)
    let array = arr
    array.forEach(c => {
        const res = arr.filter(i => i[index] !== c[index])
        arr = [...res, c]
    })
    console.log(arr)
    return arr
}

filterSameIndex(person, 'testId')

輸出結(jié)果


image.png

方法3:

let array = [
  { id: 1, name: "a" },
  { id: 3, name: "b" },
  { id: 3, name: "c" },
  { id: 2, name: "a" },
];

function filterSameKey(arr, key) {
  return arr.filter((item, index, self) => {
    return self.findIndex((el) => el[key] === item[key]) === index;
  });
}
filterSameKey(array, "id");

兩個(gè)數(shù)組合并,有相同鍵的替換,不同則添加

let arr = [
    { key: 1, name: '1-1' },
    { key: 2, name: '1-2' },
    { key: 3, name: '1-3' },
    { key: 4, name: '1-4' },
]
let arr1 = [
    { key: 1, name: '2-1' },
    { key: 3, name: '2-3' },
    { key: 5, name: '2-5' },
]
let arr2 = []
let obj = {};
arr2 = [...arr, ...arr1].reduce((cur, next) => {
    if (obj[next.key]) {
        let index = cur.findIndex(item => item.key === obj[next.key])
        cur.splice(index, 1, next)
    } else {
        // 因?yàn)閷?duì)象是儲(chǔ)存的鍵值對(duì),
        // 所以對(duì)象obj的鍵arr[i]必須對(duì)應(yīng)一個(gè)值,這個(gè)值是什么不重要,但別是undefined,因?yàn)檫@樣你就不容易判斷對(duì)象里面有沒有這個(gè)鍵。
        obj[next.key] = next.name && cur.push(next)
    }
    return cur;
}, []) //設(shè)置cur默認(rèn)類型為數(shù)組,并且初始值為空的數(shù)組

console.log('arr2', arr2)

輸出結(jié)果:


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容