Search code examples
javascriptecma

How to check object is present in JSON or not Using Javascript


I have an array of the object users.

"users" : [
  {
     fname: "subrato",
     lname:"patnaik",
     password:"123"
  },
 {
     fname: "john",
     lname:"doe",
     password:"123"    
 }
]

I want to check whether the above JSON data contain the below object.

{fname:"subrato", password:"123"}

How could we do that in Javascript?


Solution

  • You're gonna need to loop through the array and do a check for it.

    arr.forEach(obj => {
      if(obj.fname == 'name' && obj.password == 'password') {
        // Do stuff
      }
    })