
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Retrieve property value selectively from array of objects in JavaScript
Suppose, we have an array of objects like this −
const arr = [ { id : "23", name : "Item 1", isActive : true}, { id : "25", name : "Item 2", isActive : false}, { id : "26", name : "Item 3", isActive : false}, { id : "30", name : "Item 4", isActive : true}, { id : "45", name : "Item 5", isActive : true} ];
We are required to write a JavaScript function that takes in one such object and return an array of the value of "id" property of all those objects that have true value for the "isActive" property.
Example
The code for this will be −
const arr = [ { id : "23", name : "Item 1", isActive : true}, { id : "25", name : "Item 2", isActive : false}, { id : "26", name : "Item 3", isActive : false}, { id : "30", name : "Item 4", isActive : true}, { id : "45", name : "Item 5", isActive : true} ]; const findActive = (arr = []) => { const res = []; for(let i = 0; i < arr.length; i++){ const obj = arr[i]; const { id, isActive } = obj; if(isActive){ res.push(id); } }; return res; }; console.log(findActive(arr));
Output
And the output in the console will be −
[ '23', '30', '45' ]
- Related Articles
- How to selectively retrieve value from json output JavaScript
- Sort array of objects by string property value in JavaScript
- Sort array of objects by string property value - JavaScript
- Sorting an array objects by property having null value in JavaScript
- Retrieve user id from array of object - JavaScript
- Sum of array object property values in new array of objects in JavaScript
- Filter array of objects by a specific property in JavaScript?
- Add property to common items in array and array of objects - JavaScript?
- Sorting an array of objects by property values - JavaScript
- Extract a property from an array of objects in PHP
- Search from an array of objects via array of string to get array of objects in JavaScript
- Find specific key value in array of objects using JavaScript
- Extract unique objects by attribute from an array of objects in JavaScript
- Removing duplicate objects from array in JavaScript
- Fetch specific values from array of objects in JavaScript?

Advertisements