- 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 away object in array with null values JavaScript
Let’s say, we have an array of objects about some employees of a company. But the array contains some bad data i.e., key pointing to empty strings or false values. Our job is to write a function that takes in the array and away the objects that have null or undefined or empty string value for the name key and return the new object.
The array of objects are like this −
let data = [{ "name": "Ramesh Dhiman", "age": 67, "experience": 45, "description": "" }, { "name": "", "age": 31, "experience": 9, "description": "" }, { "name": "Kunal Dhiman", "age": 27, "experience": 7, "description": "" }, { "name": "Raman Kumar", "age": 34, "experience": 10, "description": "" }, { "name": "", "age": 41, "experience": 19, "description": "" } ]
Let’s write the code for this function −
Example
let data = [{ "name": "Ramesh Dhiman", "age": 67, "experience": 45, "description": "" }, { "name": "", "age": 31, "experience": 9, "description": "" }, { "name": "Kunal Dhiman", "age": 27, "experience": 7, "description": "" }, { "name": "Raman Kumar", "age": 34, "experience": 10, "description": "" }, { "name": "", "age": 41, "experience": 19, "description": "" } ] const filterUnwanted = (arr) => { const required = arr.filter(el => { return el.name; }); return required; }; console.log(filterUnwanted(data));
Output
The output in the console will be −
[ { name: 'Ramesh Dhiman', age: 67, experience: 45, description: '' }, { name: 'Kunal Dhiman', age: 27, experience: 7, description: '' }, { name: 'Raman Kumar', age: 34, experience: 10, description: '' } ]
- Related Articles
- Filter null from an array in JavaScript?
- Set JavaScript object values to null?
- Filter array with filter() and includes() in JavaScript
- Filter unique array values and sum in JavaScript
- Filter an object based on an array JavaScript
- Filter one array with another array - JavaScript
- JavaScript in filter an associative array with another array
- How to assign values to an array with null/empty objects in JavaScript?
- Filter JavaScript array of objects with another array
- Convert array with duplicate values to object with number of repeated occurrences in JavaScript
- JavaScript: How to filter out Non-Unique Values from an Array?
- How to filter values from an array using the comparator function in JavaScript?
- Looping numbers with object values and push output to an array - JavaScript?
- Sum of nested object values in Array using JavaScript
- JavaScript - convert array with null value to string

Advertisements