How to use filter() and includes() Methods In Javascript
In this method, we will filter the data from the book and include the data which is not repeated in a new array.
Example: This example is the implementation of the above-explained method.
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);
Output
[ { title: 'C++', author: 'Bjarne' }, { title: 'Python', author: 'Guido' }, { title: 'Java', author: 'James' } ]
How to Remove Duplicates from an Array of Objects in JavaScript?
This article focuses on removing duplicate object elements from the JavaScript array. We are provided with an array of objects, and our goal is to eliminate duplicate objects from the list.
These are the methods to solve this problem which are discussed below:
Table of Content
- Using One of the keys as Index
- Converting Array to Set and Remove Duplicates
- Using filter() and includes() Methods
- Using filter() and findIndex() Methods
- Using Lodash _.uniq() Method
Contact Us