Categories
JavaScript Answers

How to find if object property exists in array with JavaScript and Lodash?

Spread the love

Sometimes, we want to find if object property exists in array with JavaScript and Lodash.

In this article, we’ll look at how to find if object property exists in array with JavaScript and Lodash.

How to find if object property exists in array with JavaScript and Lodash?

To find if object property exists in array with JavaScript and Lodash, we can use the find method.

For instance, we write:

const data = [{
    "name": "apple",
    "id": "apple0"
  },
  {
    "name": "dog",
    "id": "dog1"
  },
  {
    "name": "cat",
    "id": "cat2"
  }
]

const apple = _.find(data, {
  name: 'apple'
})
console.log(apple)

to call _.find with data with the property key and value we’re looking for.

Therefore, apple is {name: 'apple', id: 'apple0'}.

Conclusion

To find if object property exists in array with JavaScript and Lodash, we can use the find method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *