How to Find Property Values in an Array of Object using if/else Condition in JavaScript ?
Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects.
Table of Content
Using Array Find Method
The array find method loop in JavaScript is used to retrieve the first element in an array that satisfies a given condition. In this approach, using the find() method, we create a function findPropertyValue() to filter objects in the ‘objectsArray’ array based on a specific object id, returning an object property value.
Syntax:
array.find(function(currentValue, index, arr), thisValue)
Example: The below code uses the JavaScript array find method to find property values in an array of objects.
const objectsArray = [
{ id: 1, name: "GeeksforGeeks" },
{ id: 2, name: "Computer Science" },
{ id: 3, name: "Portal" },
{ id: 4, name: "For Geeks" },
{ id: 5, name: "GFG" },
]
const findPropertyValue = (objectId) => {
const object =
objectsArray.find((object) => object.id === objectId);
if (object) { //If-else condition
return object.name;
}
else {
return null;
}
}
// Prints "name" property value of object with "id" 2
console.log(findPropertyValue(2));
Output
Computer Science
Using Array Filter Method
In this approach, using the filter() method, we create a function findPropertyValue() to filter objects in the ‘objectsArray’ array based on a specific object id, returning an object property value in the form of a new array containing this particular object property that satisfies the specified condition.
Syntax:
array.filter(callback(element, index, arr), thisValue)
Example: The below code uses the JavaScript array filter method to find property values in an array of objects.
const objectsArray = [
{ id: 1, name: "GeeksforGeeks" },
{ id: 2, name: "Computer Science" },
{ id: 3, name: "Portal" },
{ id: 4, name: "For Geeks" },
{ id: 5, name: "GFG" },
]
function getPropertyValue(objectId) {
const property =
objectsArray.filter(object => object.id == objectId);
if (property[0].name != null) { //If-else condition
return property[0].name;
}
else {
return;
}
}
//Prints "name" property value of object with "id" 1
console.log(getPropertyValue(1));
Output
GeeksforGeeks
Using For Of Loop
In this approach, we create a function findPropertyValue() using a for-of loop to iterate through objects based on a specific object ID, searching for a property’s value accordingly. The matching object property value is returned as the output.
Example: The below code uses the JavaScript for-of loop to find property values in an array of objects.
const objectsArray = [
{ id: 1, name: "GeeksforGeeks" },
{ id: 2, name: "Computer Science" },
{ id: 3, name: "Portal" },
{ id: 4, name: "For Geeks" },
{ id: 5, name: "GFG" },
]
function getPropertyValue(objectID) {
for (const obj of objectsArray) {
if (obj.id == objectID) { //If-else condition
return obj.name;
} else {
continue;
}
}
return null;
}
//Prints "name" property value of object with "id" 3
console.log(getPropertyValue(3));
Output
Portal
Using Array Map Method
Another useful approach to find and transform property values in an array of objects is by utilizing the map() method. The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. This can be particularly useful for extracting or transforming specific property values.
Example: In this example, we use the map() method to find and transform property values in an array of objects. The function findAndTransformPropertyValue() takes an array of objects and a specific property name, then returns a new array containing the values of the specified property for each object.
function findAndTransformPropertyValue(objectsArray, propertyName) {
return objectsArray.map(obj => obj[propertyName]);
}
const objectsArray = [
{ id: 1, name: 'Alice', age: 25 },
{ id: 2, name: 'Bob', age: 30 },
{ id: 3, name: 'Charlie', age: 35 }
];
const names = findAndTransformPropertyValue(objectsArray, 'name');
console.log(names); // Output: ['Alice', 'Bob', 'Charlie']
const ages = findAndTransformPropertyValue(objectsArray, 'age');
console.log(ages); // Output: [25, 30, 35]
Output
[ 'Alice', 'Bob', 'Charlie' ] [ 25, 30, 35 ]
Using Array Reduce Method
The reduce method executes a reducer function on each element of the array, resulting in a single output value. This method can be particularly useful for aggregating or accumulating property values based on certain conditions.
Example: The following code demonstrates how to use the JavaScript reduce method to find a property value in an array of objects. We create a function findPropertyValueUsingReduce() that accumulates the desired property value based on a specific object ID.
const objectsArray = [
{ id: 1, name: "GeeksforGeeks" },
{ id: 2, name: "Computer Science" },
{ id: 3, name: "Portal" },
{ id: 4, name: "For Geeks" },
{ id: 5, name: "GFG" },
];
const findPropertyValueUsingReduce = (objectId) => {
return objectsArray.reduce((accumulator, currentObject) => {
if (currentObject.id === objectId) { // If-else condition
return currentObject.name;
}
return accumulator;
}, null);
};
// Prints "name" property value of object with "id" 4
console.log(findPropertyValueUsingReduce(4));
Output
For Geeks
Using Array Some Method
The some method tests whether at least one element in the array passes the provided test function. This can be useful for finding a property value and stopping the search as soon as the condition is met.
Example: The following code demonstrates how to use the JavaScript some method to find a property value in an array of objects. We create a function findPropertyValueUsingSome() that returns the property value of an object based on a specific object ID if it exists.
const objectsArray = [
{ id: 1, name: "GeeksforGeeks" },
{ id: 2, name: "Computer Science" },
{ id: 3, name: "Portal" },
{ id: 4, name: "For Geeks" },
{ id: 5, name: "GFG" },
];
const findPropertyValueUsingSome = (objectId) => {
let result = null;
objectsArray.some(object => {
if (object.id === objectId) { // If-else condition
result = object.name;
return true;
}
return false;
});
return result;
};
console.log(findPropertyValueUsingSome(5));
Output
GFG