【js】javascript 数组去除重复的对象

1.只将第一个对象保留在具有属性值的数组中

要通过 JavaScript 中的属性过滤数组中的重复对象,请使用 filter() 方法过滤掉不是数组中第一个具有该属性值的元素。

const arr = [
  {
    name: 'John',
    location: 'Los Angeles',
  },
  {
    name: 'Kate',
    location: 'New York',
  },
  {
    name: 'Mike',
    location: 'New York',
  },
];
 
 
const unique = arr.filter(
  (obj, index) =>
    arr.findIndex((item) => item.location === obj.location) === index
);
/*
   [
     { name: 'John', location: 'Los Angeles' },
     { name: 'Kate', location: 'New York' }
   ]
 */
console.log('unique',unique)

Array filter() 根据回调函数指定的条件测试数组中的每个元素,并创建一个新数组,其中填充通过测试的元素,它不会修改原始数组。

const arr = [1, 2, 3, 4];
 
 
const filtered = arr.filter((num) => num > 2);
console.log(filtered); // [ 3, 4 ]
Array findIndex() 方法搜索数组中的元素并返回第一个通过测试的元素的索引,由传递给它的回调函数指定。我们使用它来查找与当前正在测试的对象 filter() 具有相同属性值的第一个数组元素。

在我们的示例中,对象的 filter() 条件是其数组索引与 findIndex() 的结果相同。如果此条件为真,则意味着该对象是具有属性值的第一个数组元素。如果为假,则意味着已经有一个具有该属性值的数组项,因此该对象是重复的,不应包含在结果中。

const arr = [
  {
    name: 'John',
    location: 'Los Angeles',
  },
  {
    name: 'Kate',
    location: 'New York',
  },
  {
    name: 'Mike',
    location: 'New York',
  },
];
 
 
// true - first object with location of New York
console.log(1 === arr.findIndex((obj) => obj.location === 'New York'));
 
 
// false - will not be included in result
console.log(2 === arr.findIndex((obj) => obj.location === 'New York'));

按多个属性过滤数组中的重复对象

根据您的情况,您可能希望仅当对象具有两个或多个具有相同值的属性时才将其视为重复对象——多个属性值相同。

对于这种情况,我们将像以前一样使用 filter() 和 findIndex(),但我们将为所有属性添加 filter() 的对象和 findIndex() 的对象之间的额外比较。

const arr = [
  {
    name: 'Kate',
    location: 'New York'
  },
  {
    name: 'Mike',
    location: 'New York'
  },
  {
    name: 'Kate',
    location: 'New York'
  }
];
 
 
const unique = arr.filter(
  (obj, index) =>
    arr.findIndex(
      (item) => item.location === obj.location && item.name === obj.name
    ) === index
)
/*
   [
      { name: 'Kate', location: 'New York' },
      { name: 'Mike', location: 'New York' }
   ]
 */
console.log('unique',unique)

2.从唯一数组中排除重复项

这是在 JavaScript 中从数组中过滤重复对象的另一种方法:

创建一个空的唯一数组来存储唯一对象。

循环遍历数组中的对象。

对于每个对象,如果它不是重复的,则将其添加到唯一数组。否则,忽略它。

const arr = [
  {
    name: 'John',
    location: 'Los Angeles',
  },
  {
    name: 'Kate',
    location: 'New York',
  },
  {
    name: 'Mike',
    location: 'New York',
  },
];
 
 
const unique = [];
for (const item of arr) {
  const isDuplicate = unique.find((obj) => obj.location === item.location);
  if (!isDuplicate) {
    unique.push(item);
  }
}
/*
   [
    { name: 'John', location: 'Los Angeles' },
    { name: 'Kate', location: 'New York' }
  ]
*/
console.log('unique',unique)

我们使用 for...of 循环遍历数组并对每个对象执行一个操作。

对于每个对象,我们使用 find() 方法检查它是否已经存在于唯一数组中。Array find() 搜索数组中第一个通过指定测试的对象,类似于 findIndex(),但它返回对象本身而不是它的数组索引。

const nums = [2, 5, 8, 13, 19];
 
 
const doubleDigit = nums.find((num) => num > 9);
console.log(doubleDigit); // 13
如果它不在唯一数组中,我们只需使用 push() 方法添加它。

这种方法不像第一种方法那样单行,但您可能会发现它更容易理解。这似乎是您作为人类从列表中删除重复项目的自然方式。

3.按多个属性过滤数组中的重复对象

与前面的方法一样,如果使用多个属性来确定一个对象是否重复,您可以简单地为属性添加更多检查——这次是在 find() 方法中:

const arr = [
  {
    name: 'Kate',
    location: 'New York',
  },
  {
    name: 'Mike',
    location: 'New York',
  },
  {
    name: 'Kate',
    location: 'New York',
  },
];
 
 
const unique = [];
for (const item of arr) {
  // 👇 "name" and "location" used for duplicate check
  const duplicate = unique.find(
    (obj) => obj.location === item.location && obj.name === item.name
  );
  if (!duplicate) {
    unique.push(item);
  }
}
/*
  [
    { name: 'Kate', location: 'New York' },
    { name: 'Mike', location: 'New York' }
  ]
 */
console.log(unique);
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值