- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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' ]
Advertisements