jquery 删除javascript上具有相同值的所有对象

uxh89sit  于 2022-11-29  发布在  jQuery
关注(0)|答案(4)|浏览(146)

你好,我是新的编程,我有一个系统上的问题,我试图使预订系统。
我想删除一个数组中具有相同ID的对象块,同时单击一个按钮。

array = [
{ id: 1, futsal: "4", time: "06:00 - 09:00", … },
​
{ id: 1, futsal: "4", time: "06:00 - 09:00", … },
​
{ id: 1, futsal: "4", time: "06:00 - 09:00", … },

{ id: 2, futsal: "4", time: "07:00 - 08:00", … },
​
{ id: 2, futsal: "4", time: "07:00 - 07:00", … },
​
{ id: 3, futsal: "4", time: "08:00 - 09:00", … },
​
{ id: 3, futsal: "4", time: "08:00 - 09:00", … },
​
{ id: 3, futsal: "4", time: "08:00 - 09:00", … }]

我想一次删除所有ID相同的对象,即ID=1或2的所有对象...

72qzrwbm

72qzrwbm1#

const uniqueIds = [];

  const unique = arr.filter(element => {
  const isDuplicate = uniqueIds.includes(element.id);

  if (!isDuplicate) {
    uniqueIds.push(element.id);

    return true;
  }

    return false;
  });

  // 👇️ 
  console.log(unique);
qrjkbowd

qrjkbowd2#

有几种方法可以取得筛选的对象。

{ id: 1, futsal: "4", time: "06:00 - 09:00", },
    { id: 1, futsal: "4", time: "06:00 - 09:00", },
    { id: 1, futsal: "4", time: "06:00 - 09:00", },
    { id: 2, futsal: "4", time: "07:00 - 08:00", },
    { id: 2, futsal: "4", time: "07:00 - 07:00", },
    { id: 3, futsal: "4", time: "08:00 - 09:00", },
    { id: 3, futsal: "4", time: "08:00 - 09:00", },
    { id: 3, futsal: "4", time: "08:00 - 09:00", }
];

选项优先

const itemUIds = items.map(o => o.id)
const filteredOption1 = items.filter(({id}, index) => !itemUIds.includes(id, index + 1));
console.log(filteredOption1)

第二个选项

const filteredOption2 = Object.values(items.reduce((acc,cur)=>Object.assign(acc,{[cur.id]:cur}),{}));
console.log(filteredOption2)

选项三

const uniqueItem:any = {};
const filteredOption3 = items.filter(obj => !uniqueItem[obj.id] && (uniqueItem[obj.id] = true));
console.log(filteredOption3);
cqoc49vn

cqoc49vn3#

如果您要将id作为唯一键进行检查,则可以使用ES6功能按键获取对象的唯一列表。

newArr = [...new Map(array.map(item => [item["id"], item])).values()]// I am passing `id` as key
array = [
{ id: 1, futsal: "4", time: "06:00 - 09:00"},
{ id: 1, futsal: "4", time: "06:00 - 09:00"},
{ id: 1, futsal: "4", time: "06:00 - 09:00"},
{ id: 2, futsal: "4", time: "07:00 - 08:00"},
{ id: 2, futsal: "4", time: "07:00 - 07:00"},
{ id: 3, futsal: "4", time: "08:00 - 09:00"},
{ id: 3, futsal: "4", time: "08:00 - 09:00"},
{ id: 3, futsal: "4", time: "08:00 - 09:00"}]

newArr = [...new Map(array.map(item => [item["id"], item])).values()]

console.log(newArr);
jk9hmnmh

jk9hmnmh4#

您可以使用filter an array制作一个不含不需要元素的副本:

const array = [
  { id: 1, futsal: "4", time: "06:00 - 09:00" },
  { id: 1, futsal: "4", time: "06:00 - 09:00" },
  { id: 1, futsal: "4", time: "06:00 - 09:00" },
  { id: 2, futsal: "4", time: "07:00 - 08:00" },
  { id: 2, futsal: "4", time: "07:00 - 07:00" },
  { id: 3, futsal: "4", time: "08:00 - 09:00" },
  { id: 3, futsal: "4", time: "08:00 - 09:00" },
  { id: 3, futsal: "4", time: "08:00 - 09:00" }
];

// only items where id is not 2
const filtered = array.filter(item => item.id !== 2);

console.log(filtered);

相关问题