-2

I have an object as follows -

var users = {
  room: [1,2,3,4]
}

How do I find if the 3 exists in the room array ?

JS bin

Ajey
  • 7,924
  • 12
  • 62
  • 86

1 Answers1

1

Use indexOf:

var indexOfThree = users.room.indexOf(3);
if(indexOfThree != -1)
{
   var three = users.room[indexOfThree];
}
else
{
   console.log("not found");
}

it will return -1 if the element isn't found or else it's index in the array.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99