JavaScript 如何从对象数组中删除重复项

JavaScript 如何从对象数组中删除重复项

在本文中,我们将给出一个对象数组,并从数组中删除重复的对象元素。

以下是解决这个问题的方法:

  • 使用其中一个键作为索引
  • 将数组转换为Set以删除重复项
  • 使用filter()和includes()方法
  • 使用filter()和findIndex()方法

方法1:使用其中一个键作为索引

创建一个临时数组,使用原始数组中的一个键存储对象。可以使用对象的任何属性作为键。从对象中提取键,并将其用作新临时数组的索引。然后将该对象分配给此索引。这种方法将删除重复的对象,因为原始数组中的每个对象只会分配给相同的索引。

示例: 在此示例中,我们将使用其中一个键作为索引从对象数组中删除重复的值。

function removeDuplicates() {
 
    // Create an array of objects
    books = [
        { title: "C++", author: "Bjarne" },
        { title: "Java", author: "James" },
        { title: "Python", author: "Guido" },
        { title: "Java", author: "James" },
    ];
 
    // Declare a new array
    let newArray = [];
 
    // Declare an empty object
    let uniqueObject = {};
 
    // Loop for the array elements
    for (let i in books) {
 
        // Extract the title
        objTitle = books[i]['title'];
 
        // Use the title as the index
        uniqueObject[objTitle] = books[i];
    }
 
    // Loop to push unique object into array
    for (i in uniqueObject) {
        newArray.push(uniqueObject[i]);
    }
 
    // Display the unique objects
    console.log(newArray);
}
removeDuplicates();

输出

[
  { title: 'C++', author: 'Bjarne' },
  { title: 'Java', author: 'James' },
  { title: 'Python', author: 'Guido' }
]

方法2:将数组转换为集合以删除重复值

Set对象仅包含任何类型的唯一值。这个属性可以用来仅存储数组中唯一的对象。每个数组对象首先使用JSON.stringify方法转换为JSON编码字符串。然后使用map()方法将JSON编码字符串映射到数组中。通过将这个数组作为参数传递给新的set构造函数来创建一个新的set。这一步将删除所有重复的元素,因为相同元素的JSON编码字符串将是相同的。然后使用from()方法将set转换为数组,将set作为参数传递。这个数组将不会有重复的对象。

示例: 在这个示例中,我们将通过将数组转换为set来删除数组中的重复值。

function removeDuplicates() {
 
    // Create an array of objects
    books = [
        { title: "C++", author: "Bjarne" },
        { title: "Java", author: "James" },
        { title: "Python", author: "Guido" },
        { title: "Java", author: "James" },
    ];
 
    jsonObject = books.map(JSON.stringify);
    uniqueSet = new Set(jsonObject);
    uniqueArray = Array.from(uniqueSet).map(JSON.parse);
 
    console.log(uniqueArray);
}
removeDuplicates();

输出

[
  { title: 'C++', author: 'Bjarne' },
  { title: 'Java', author: 'James' },
  { title: 'Python', author: 'Guido' }
]

方法3:使用filter()和includes()方法

在这种方法中,我们将从书籍中过滤数据并将不重复的数据包含在一个新的数组中。

示例:

const books = [
    { title: "C++", author: "Bjarne" },
    { title: "Java", author: "James" },
    { title: "Python", author: "Guido" },
    { title: "Java", author: "James" },
];
 
const ids = books.map(({ title }) => title);
const filtered = books.filter(({ title }, index) =>
    !ids.includes(title, index + 1));
 
console.log(filtered);

输出

[
  { title: 'C++', author: 'Bjarne' },
  { title: 'Python', author: 'Guido' },
  { title: 'Java', author: 'James' }
]

方法4:使用filter()和findIndex()方法

在这个方法中,我们将使用filter()方法根据提供的测试函数来过滤数组,并使用Array.findIndex()方法返回给定数组中满足条件的第一个元素的索引(用户在调用时传入)。

示例:

const books = [
    { title: "C++", author: "Bjarne" },
    { title: "Java", author: "James" },
    { title: "Python", author: "Guido" },
    { title: "Java", author: "James" },
];
 
const unique = books.filter((obj, index) => {
    return index === books.findIndex(o => obj.title === o.title);
});
 
console.log(unique);

输出

[
  { title: 'C++', author: 'Bjarne' },
  { title: 'Java', author: 'James' },
  { title: 'Python', author: 'Guido' }
]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程