JavaScript
Array Manipulation
Object Attributes
Coding Tutorial
Programming Tips

How to determine if a JavaScript array contains an object with an attribute that equals a given value

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you only need to know whether at least one object in an array has a property equal to a target value, the most direct JavaScript tool is some. If you also want the matching object, use find. The right method depends on whether you want a boolean answer or the object itself.

Use some for a Boolean Answer

some stops as soon as a match is found and returns true or false.

javascript
1const users = [
2  { id: 1, name: "John" },
3  { id: 2, name: "Jane" },
4  { id: 3, name: "Mira" },
5];
6
7const hasJane = users.some(user => user.name === "Jane");
8console.log(hasJane);

This is usually the cleanest answer when the real question is “Does such an object exist?” rather than “Which object is it?”

Use find When You Need the Matching Object

If you want the first matching object, use find instead.

javascript
1const users = [
2  { id: 1, name: "John" },
3  { id: 2, name: "Jane" },
4  { id: 3, name: "Mira" },
5];
6
7const match = users.find(user => user.name === "Jane");
8console.log(match);

If no match exists, find returns undefined.

You can convert the result to a boolean if needed:

javascript
const exists = users.find(user => user.name === "Jane") !== undefined;
console.log(exists);

But if a boolean is the only goal, some expresses the intent more clearly.

Use filter Only When You Need All Matches

filter returns every matching element, not just the first one.

javascript
1const users = [
2  { id: 1, role: "admin" },
3  { id: 2, role: "user" },
4  { id: 3, role: "admin" },
5];
6
7const admins = users.filter(user => user.role === "admin");
8console.log(admins);

This is useful when duplicates are expected and all matches matter. It is not the best choice when you only want to know whether one match exists, because it builds a new array unnecessarily.

Check Dynamic Property Names

Sometimes the property name is not hardcoded. In that case, use bracket notation.

javascript
1const items = [
2  { sku: "A1", status: "active" },
3  { sku: "B2", status: "inactive" },
4];
5
6const key = "status";
7const target = "active";
8
9const exists = items.some(item => item[key] === target);
10console.log(exists);

This is especially useful in reusable helpers.

A Reusable Helper Function

If you perform this check often, a small helper can make the call sites cleaner.

javascript
1function containsByProperty(array, property, value) {
2  return array.some(item => item?.[property] === value);
3}
4
5const products = [
6  { id: 10, category: "books" },
7  { id: 20, category: "games" },
8];
9
10console.log(containsByProperty(products, "category", "games"));

The optional chaining protects against null or undefined elements inside the array.

Case Sensitivity and Type Equality

JavaScript comparisons are case-sensitive for strings and strict equality is usually the right choice.

javascript
const users = [{ name: "Jane" }];
console.log(users.some(user => user.name === "jane"));

That prints false.

If you want case-insensitive matching, normalize both sides.

javascript
const exists = users.some(
  user => user.name.toLowerCase() === "jane".toLowerCase()
);

Likewise, use === unless you explicitly want type coercion. Otherwise values like 1 and "1" may be treated as equal in ways that create bugs.

Nested Attributes Need Explicit Access

If the property lives inside another object, access it explicitly and guard against missing intermediate values.

javascript
1const orders = [
2  { id: 1, customer: { name: "Ava" } },
3  { id: 2, customer: { name: "Leo" } },
4];
5
6const hasAva = orders.some(order => order.customer?.name === "Ava");
7console.log(hasAva);

Optional chaining keeps the check safe even if some objects do not have a customer field.

Common Pitfalls

A common mistake is using filter when only a boolean answer is needed. some is simpler and avoids building an unnecessary array.

Another mistake is using find and forgetting that it returns undefined when nothing matches.

Developers also often forget that string comparisons are case-sensitive and that strict equality is usually safer than loose equality.

Finally, if the property name is dynamic or nested, use bracket notation or optional chaining instead of assuming every object has the same exact shape.

Summary

  • Use some when you need a boolean answer.
  • Use find when you need the first matching object.
  • Use filter only when you need all matching objects.
  • Use bracket notation for dynamic property names and optional chaining for nested values.
  • Prefer strict equality and explicit normalization when matching strings or mixed types.

Course illustration
Course illustration

All Rights Reserved.