Navigate back to the homepage

How to check value exist in array of object in javascript?

Infinitbility
JavaScript
November 4th, 2021 · 1 min read
How to check value exist in array of object in javascript?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to check value exist in array of objects in javascript, for check value exist or not we will use below methods.

  1. Javascript findIndex() method
  2. Javascript find() method

Let’s start today’s tutorial How to check value exist in array of object in javascript?

Javascript findIndex() method

Javascript provide findIndex() method to find value in array, and we can also use this method to check value exist or not in array of objects.

Let take below users array of objects for example.

1const users = [
2 {
3 id: 1,
4 name: "Infinit"
5 },{
6 id: 2,
7 name: "Bility"
8 },{
9 id: 3,
10 name: "Infinitbility"
11 }
12];

let check users exist or not using user id value.

1const users = [
2 {
3 id: 1,
4 name: "Infinit"
5 },{
6 id: 2,
7 name: "Bility"
8 },{
9 id: 3,
10 name: "Infinitbility"
11 }
12];
13
14let userIndex = users.findIndex(e => e.id == 2);
15console.log(userIndex); // 1
16if(userIndex >= 0){
17 console.log("Exist");
18} else {
19 console.log("Not exist");
20}

javascript findIndex() method return index of object if value match else it will return -1.

output

findIndex, Javascript, Example

Javascript find() method

Javascript find() method same work like findIndex but it will return whole object or array value if match else it will return undefined.

1const users = [
2 {
3 id: 1,
4 name: "Infinit"
5 },{
6 id: 2,
7 name: "Bility"
8 },{
9 id: 3,
10 name: "Infinitbility"
11 }
12];
13
14let user = users.find(e => e.id == 2);
15console.log(user); // { id: 2, name: 'Bility' }
16if(user){
17 console.log("Exist");
18} else {
19 console.log("Not exist");
20}

output

find, Javascript, Example

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to check value is decimal or not in javascript?

How to check value is decimal or not in javascript?

Example of check value is decimal or not in javascript, React, React Native, vue, node, deno, and all javascript framework.

November 3rd, 2021 · 1 min read
How to check value is number or not in javascript?

How to check value is number or not in javascript?

Example of check value is number or not in javascript, React, React Native, vue, node, deno, and all javascript framework.

November 2nd, 2021 · 1 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]