- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Filter array with filter() and includes() in JavaScript
Let’s say following is our array −
const names = ['John', 'David', 'Mike','Sam','Carol','Bob'];
Now, we will filter the array −
var nameObject=names.filter((allNameObject) => !['David', 'Mike','Sam','Carol'].includes(allNameObject));
Example
const names = ['John', 'David', 'Mike','Sam','Carol','Bob']; console.log("The names are="); console.log(names); var nameObject=names.filter((allNameObject) => !['David', 'Mike','Sam','Carol'].includes(allNameObject)); console.log("After filter="); console.log(nameObject);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo65.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo65.js The names are= [ 'John', 'David', 'Mike', 'Sam', 'Carol', 'Bob' ] After filter= [ 'John', 'Bob' ]
- Related Articles
- Filter one array with another array - JavaScript
- Filter JavaScript array of objects with another array
- JavaScript in filter an associative array with another array
- Filter unique array values and sum in JavaScript
- Filter away object in array with null values JavaScript
- Filter array based on another array in JavaScript
- JavaScript filter array by multiple strings?
- Filter null from an array in JavaScript?
- Nested collection filter with JavaScript
- Remove elements from array using JavaScript filter - JavaScript
- Remove/ filter duplicate records from array - JavaScript?
- How to filter array in subdocument with MongoDB?
- Filter an object based on an array JavaScript
- How to filter out common array in array of arrays in JavaScript
- Filter array of objects by a specific property in JavaScript?

Advertisements