乐趣区

关于javascript:JavaScript对象类型数组-for-of-去重

const arr = [{ i: 1, n: "123"},
  {i: 1, n: "123"},
  {i: 2, n: "222"},
  {i: 2, n: "222"},
  {i: 3, n: "333"}
]

// 去重
let obj = {}, newArr = []

for (const key of arr) {if (!obj[key.i]) {obj[key.i] = 1 // 标记反复对象
    newArr.push(key)
  }
}

console.log(newArr);
退出移动版