How to Find Property Values in an Array of Object using if/else Condition in JavaScript ?
Last Updated :
17 Jul, 2024
Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects.
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.
JavaScript
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));
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.
JavaScript
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));
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.
JavaScript
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));
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.
JavaScript
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.
JavaScript
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));
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.
JavaScript
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));
Similar Reads
How to find property values from an array containing multiple objects in JavaScript ?
To find property values from an array containing multiple objects in JavaScript, we have multiple approaches. In this article, we will learn how to find property values from an array containing multiple objects in JavaScript. Several methods can be used to find property values from an array containi
4 min read
Sort an array of objects using Boolean property in JavaScript
Given the JavaScript array containing Boolean values. The task is to sort the array on the basis of Boolean value with the help of JavaScript. There are two approaches that are discussed below: Table of Content Using Array.sort() Method and === OperatorUsing Array.sort() and reverse() MethodsUsing a
2 min read
How to find if an array contains a specific string in JavaScript/jQuery ?
In order to Check if an array contains a specific string be done in many ways in Javascript. One of the most common methods to do this is by using the traditional for loop. In Javascript, we have several other modern approaches which help in finding a specific string in an array to iterate the array
5 min read
How to get property descriptors of an object in JavaScript ?
Here, we are going to discuss the property descriptors of an Object in JavaScript. The Object.getOwnPropertyDescriptor() method returns an object describing a specific property on a given object. A JavaScript object can be created in many ways and its properties can be called by using property descr
2 min read
How to Find & Update Values in an Array of Objects using Lodash ?
To find and update values in an array of objects using Lodash, we employ utility functions like find or findIndex to iterate over the elements. These functions facilitate targeted updates based on specific conditions, enhancing data manipulation capabilities.Table of ContentUsing find and assign Fun
4 min read
How to Return an Array of Unique Objects in JavaScript ?
Returning an array of unique objects consists of creating a new array that contains unique elements from an original array. We have been given an array of objects, our task is to extract or return an array that consists of unique objects using JavaScript approaches. There are various approaches thro
5 min read
How to Detect an Undefined Object Property in JavaScript ?
Detecting an undefined object property is the process of determining whether an object contains a certain property, and if it does, whether the value of that property is undefined. This is an important concept in JavaScript programming, as it helps to prevent errors that can occur when attempting to
3 min read
How to get Values from Specific Objects an Array in JavaScript ?
In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using the forEach() methodUsing the map() methodUsing the filte
2 min read
How to check if the provided value is an object created by the Object constructor in JavaScript ?
In this article, we will learn how to check if the provided value is an object created by the Object constructor in JavaScript. Almost all the values in JavaScript are objects except primitive values. There are several methods that can be used to check if the provided value is an object created by t
3 min read
How Check if object value exists not add a new object to array using JavaScript ?
In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = {
3 min read