Determine if a JavaScript array contains an object with a specific attribute
This post will discuss how to determine if a JavaScript array contains an object with a specific attribute.
1. Using Array.some() function
The recommended solution is to use the Array.some() function, which is a built-in function that returns true if at least one element in the array passes a test function, and false otherwise. To use this function, we need to pass a function that checks if the object has the given attribute with the desired value. For example, the following code finds a person living in the state NYC.
|
1 2 3 4 5 6 7 8 9 |
const cities = [ { city : 'New York City', state : 'NYC', zip : '10001' }, { city : 'Colorado', state : 'CO', zip : '80011' }, { city : 'Michigan', state : 'MI', zip : '48002' } ]; // Check if the array contains an object with state equal to "NYC" const isPresent = cities.some(e => e.state === 'NYC'); console.log(isPresent); // true |
2. Using Array.find() or Array.findIndex() function
Another solution is to use the Array.find(), which is a built-in function that returns the first element in the array that passes a test function, or undefined if none is found. To use this function, we need to pass a function that checks if the object has the given attribute with the desired value. We can then use a truthy or falsy check to determine if the result is defined or not.
|
1 2 3 4 5 6 7 8 9 |
const cities = [ { city : 'New York City', state : 'NYC', zip : '10001' }, { city : 'Colorado', state : 'CO', zip : '80011' }, { city : 'Michigan', state : 'MI', zip : '48002' } ]; // Check if the array contains an object with state equal to "NYC" const isPresent = !!cities.find(e => e.state === 'NYC'); console.log(isPresent); // true |
We can also use the Array.findIndex() function, which is similar to the Array.find() function, except it returns the index of the first instance of an object or -1 if the object is not found.
|
1 2 3 4 5 6 7 8 9 |
const cities = [ { city : 'New York City', state : 'NYC', zip : '10001' }, { city : 'Colorado', state : 'CO', zip : '80011' }, { city : 'Michigan', state : 'MI', zip : '48002' } ]; // Check if the array contains an object with state equal to "NYC" const isPresent = cities.findIndex(e => e.state === 'NYC') !== -1; console.log(isPresent); // true |
3. Using Array.filter() function
Another plausible way is to filter the array to return all objects that pass the specified condition. This can be easily done using the Array.filter() function, which is a built-in function that returns a new array with all the elements in the original array that pass a test function. To use this function, we need to pass a function that checks if the object has the given attribute with the desired value. We can then check the length of the resulting array to see if it is greater than zero or not.
|
1 2 3 4 5 6 7 8 9 |
const cities = [ { city : 'New York City', state : 'NYC', zip : '10001' }, { city : 'Colorado', state : 'CO', zip : '80011' }, { city : 'Michigan', state : 'MI', zip : '48002' } ]; // Check if the array contains an object with state equal to "NYC" const isPresent = cities.filter(e => e.state === 'NYC').length > 0; console.log(isPresent); // true |
4. Using Array.forEach() function
Finally, we can iterate over the array using the Array.forEach() function and check the presence of the object in the array. The Array.forEach() function is a built-in function that executes a callback function for each element in the array. To use this function, we need to pass a callback function that checks if the object has the given attribute with the desired value. We also need to declare a variable to store the result of the check.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const cities = [ { city : 'New York City', state : 'NYC', zip : '10001' }, { city : 'Colorado', state : 'CO', zip : '80011' }, { city : 'Michigan', state : 'MI', zip : '48002' } ]; // Check if the array contains an object with state equal to "NYC" let isPresent = false; cities.forEach(o => { if (e => e.state === 'NYC') { isPresent = true; } }); console.log(isPresent); // true |
That’s all about determining if a JavaScript array contains an object with a specific attribute.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)