How to Find the First Match in an Array of Objects Using JavaScript?

Estimated read time 1 min read

Here’s one way to find the first match in an array of objects using JavaScript:

function firstMatch(arr, key, target) {
  for (let object of arr) {
    if (object[key] === target) {
      return object;
  return null;

In this solution, we use a for loop to iterate over the objects in the arr array. For each object, we check if the value of the property specified by key is equal to the target using the === operator. If it is, we return the object as it is the first match. If we reach the end of the loop, we return null as there are no matches.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply