json数据检查是否重复相同的ID

时间:2018-09-06 14:14:29

标签: javascript jquery arrays json filter

我有一个json数据,其中一个id具有不同的color_id。 所以从那里我只想检查是否重复相同的ID,然后只保留第一个

这是我的示例JSON

var data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" },
    { "id": "1", "name": "yyy", "age": "15","color_id": "1" },
    { "id": "5", "name": "zzz", "age": "59","color_id": "22" }];

我想要的输出

var data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" },
    { "id": "5", "name": "zzz", "age": "59","color_id": "22" }];

我尝试过reduce,但是我发现在那里修改了数据结构,所以我不确定是否会得到想要的输出。

4 个答案:

答案 0 :(得分:4)

var data = [{
    "id": "1",
    "name": "xxx",
    "age": "22",
    "color_id": "22"
  },
  {
    "id": "1",
    "name": "yyy",
    "age": "15",
    "color_id": "1"
  },
  {
    "id": "5",
    "name": "zzz",
    "age": "59",
    "color_id": "22"
  }
];

let map = {};
let uniqueEntries = data.filter((el) => map[el.id] ? false : map[el.id] = true);
console.log(uniqueEntries )

说明:

  1. 您创建了一个用于存储ID的地图。
  2. 然后过滤数组,每次我们找到不在地图中的条目时,都会将其添加到列表中并返回true。如果我们已经在列表中添加了它,则返回false以丢弃该条目。

条件的最后一部分使用赋值返回赋值的事实。

答案 1 :(得分:1)

您可以使用reduce创建一个新数组,并在这个新数组中使用findIndex检查该新数组是否具有具有相同ID的对象。如果存在一个具有相同ID的对象,则不要推送另一个具有相同ID的对象

var data = [{
    "id": "1",
    "name": "xxx",
    "age": "22",
    "color_id": "22"
  },
  {
    "id": "1",
    "name": "yyy",
    "age": "15",
    "color_id": "1"
  },
  {
    "id": "5",
    "name": "zzz",
    "age": "59",
    "color_id": "22"
  }
];
let m = data.reduce(function(acc, curr) {
  let findIndex = acc.findIndex(function(item) {
    return item.id === curr.id
  })
  if (findIndex === -1) {
    acc.push(curr)

  }
  return acc;
}, [])

console.log(m)

答案 2 :(得分:1)

使用Array.reduceArray.some

const data = [{
    id: '1',
    name: 'xxx',
    age: '22',
    color_id: '22',
  },
  {
    id: '1',
    name: 'yyy',
    age: '15',
    color_id: '1',
  },
  {
    id: '5',
    name: 'zzz',
    age: '59',
    color_id: '22',
  },
];

const reduced = data.reduce((tmp, x) => {
  if (tmp.some(y => y.id === x.id)) return tmp;

  return [
    ...tmp,

    x,
  ];
}, []);

console.log(reduced);


或者Array.filter,因为它是@JGoodgive的一个好主意,但有点不同

    const data = [{
        id: '1',
        name: 'xxx',
        age: '22',
        color_id: '22',
      },
      {
        id: '1',
        name: 'yyy',
        age: '15',
        color_id: '1',
      },
      {
        id: '5',
        name: 'zzz',
        age: '59',
        color_id: '22',
      },
    ];

    const reduced = data.filter((x, xi) => !data.slice(0, xi).some(y => y.id === x.id));

    console.log(reduced);

答案 3 :(得分:0)

您可以使用Set来跟踪已处理的ID。

const
  // The data set with non-unique IDs
  data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" }, { "id": "1", "name": "yyy", "age": "15","color_id": "1" }, { "id": "5", "name": "zzz", "age": "59","color_id": "22" }];
  
function dedupe(items) {
  // Create a set to keep track of IDs already encountered.
  const
    idSet = new Set();
  // Filter the items, when an ID isn't in the set add it to the set and return true
  // so item is in the result array. When the ID is in the set return false so the 
  // item will be dropped.
  return items.filter(item => {
    // If the ID is already in the set, drop it from the result. This way only the
    // first item with an ID is added to the result.
    if (idSet.has(item.id)) {
      return false;
    }
    
    // Add the ID to the set, this way we keep track of the IDs already encountered.
    idSet.add(item.id);
    // Return true so the item is included in the result array.
    return true;
  });
}    

console.log(dedupe(data));