Find Data in JS Array

I have an array:

const chats = [
  {users: ["test1", "test2"], isTyping: false},
  {users: ["test1", "test4"], isTyping: false},
]

What I want to do is return the full chat ({users: [“test1”, “test2”], isTyping: false},) based on if the users array contains “test1” or whatever other user.

So, if the users array contains test2, then I want the return to be:

{users: ["test1", "test2"], isTyping: false},

Would appreciate help on this.

I am using React.js.

There are two array methods includes and indexOf that could be used to check if the users array contains a particular value.

1 Like