js 获取数组中的重复项

数组如下

[
{checked: true, index: 0}
{checked: true, index: 1}
{checked: true, index: 2}
{checked: true, index: 3}
{checked: true, index: 4}
{checked: true, index: 5}
{checked: true, index: 1}
]

输出结果为

[checked:true, index: 1]
阅读 3.4k
2 个回答
const arr = [
  {checked: true, index: 0},
  {checked: true, index: 1},
  {checked: true, index: 2},
  {checked: true, index: 3},
  {checked: true, index: 4},
  {checked: true, index: 5},
  {checked: true, index: 1}
]

const mapper = {}
const newArr = []

for (let item of arr) {
  const {index, checked} = item
  if (mapper[index]) {
    newArr.push(item)
  } else {
    mapper[index] = true
  }
}

console.log(newArr)
const a = [
    {checked: true, index: 0},
    {checked: true, index: 1},
    {checked: true, index: 2},
    {checked: true, index: 3},
    {checked: true, index: 4},
    {checked: true, index: 5},
    {checked: true, index: 1}
]

const result = Array.from(a.reduce((m, t) => m.set(t.place, t), new Map()).values());
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题