Open In App

How to Check Presence of a Specific Object Property in an Array ?

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Checking for the presence of a specific object property in an array involves determining whether any object within the array possesses the specified property. This task is commonly encountered when working with arrays of objects and needing to verify if a particular property exists across any of the objects in the array.

Below are the methods to check the presence of a specific property in an array:

Iterative Approach

In this approach, we iterate through the array and inspect each object individually to verify if the desired property exists. This method involves looping over each object and employing conditional statements to ascertain the presence of the property.

Example: The below code checks the presence of a specific object property in an array using the iterative approach in JavaScript.

Javascript
function hasProperty(array, property) {
    for (let i = 0; i < array.length; i++) {
        if (array[i].hasOwnProperty(property)) {
            return true;
        }
    }
    return false;
}

const data = [
    { id: 1, name: "John" },
    { id: 2, name: "Alice", age: 25 },
    { id: 3, name: "Bob", city: "New York" }
];

console.log(hasProperty(data, "age")); 
console.log(hasProperty(data, "email")); 

Output
true
false

Using Array.prototype.some()

In this approach, we leverage the some() method provided by arrays to determine if at least one object within the array possesses the specified property. The some() method iterates through each element of the array until it encounters an object with the desired property.

Example: The below code check presence of a specific object property in an array using the Array.prototype.some() approach in JavaScript.

Javascript
function hasProperty(array, property) {
    return array.some(obj => obj
        .hasOwnProperty(property));
}

const data = [
    { id: 1, name: "John" },
    { id: 2, name: "Alice", age: 25 },
    { id: 3, name: "Bob", city: "New York" }
];

console.log(hasProperty(data, "age"));
console.log(hasProperty(data, "email"));

Output
true
false

Similar Reads

Spectre Avatar presence
Spectre Avatar presence is used to set the indicator of the presences. We can add an &lt;i&gt; element and place a avatar-presence class inside that element and add online, away or busy whatever you want to show. Spectre Avatar presence Class: avatar-presence: This class is used to render the presence layout in the avatar.online: This class is used
2 min read
How to Check if an Object has a Specific Property in JavaScript ?
In JavaScript, objects can have properties that store data or functions. Sometimes it is necessary to check whether an object has a specific property. This can be useful, for example, when you want to avoid calling a function on an object that doesn't have that function defined as a property. In this article, we will be discussing different approac
3 min read
How to check if an object has a specific property in JavaScript?
In JavaScript, you can check if an object has a specific property using various methods, such as the hasOwnProperty() method, the in operator, or using the Object.keys() method. Here's how you can achieve it using these methods: Table of Content Using the hasOwnProperty() methodUsing the in operatorUsing the Object.keys() methodUsing the hasOwnProp
2 min read
How to use array that include and check an object against a property of an object ?
Array.includes() Method: In JavaScript, includes() method is used to determine that a particular element is present in an array or not. It returns true if the element is present and false when it is absent. Syntax: array_name.includes(searchElement, ?fromIndex) Parameters: searchElement: The element to be search in the array.fromIndex: The index fr
3 min read
How to Replace an Object in an Array with Another Object Based on Property ?
In JavaScript, replacing an object in an array with another object based on a specific property involves identifying and updating elements within the array. This task is essential when modifications to individual objects are required, such as updating values or swapping objects based on a particular property criteria. Table of Content Using map()Us
3 min read
How to Replace an Object in an Array with Another Object Based on Property ?
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 findIndex and splice methodsUsing filter and concat methodsUsing the map methodUsing findIndex and splice m
3 min read
How To Remove Specific JSON Object From Array JavaScript?
Removing Specific JSON objects from the array is important because it allows for targeted data manipulation and maintenance within JavaScript applications. we will learn to Remove Specific JSON Object From Array Javascript. These are the following methods: Table of Content Using filter() methodUsing splice() methodUsing for loop Using filter() meth
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 = { id: 1, name: ''Geeks1" }, { id: 2, name: '"Geeks2"
3 min read
How to Search Character in List of Array Object Inside an Array Object in JavaScript ?
Searching for a character in a list of array objects inside an array object involves inspecting each array object, and then examining each array within for the presence of the specified character. The goal is to determine if the character exists within any of the nested arrays. There are several ways to search for characters in a list of array obje
4 min read
How to compare two objects to determine the first object contains equivalent property values to the second object in JavaScript ?
In this article, we are going to learn about comparing two objects to determine if the first object contains equivalent property values to the second object, In JavaScript, comparing the values of two objects involves checking if they have the same properties with corresponding values. Given two objects obj1 and obj2 and the task are to check that
5 min read