-1

I want to return true if a nested array contains a particular value

In this example I'm trying to see if the users array has the current users id but I get the object instead of true

var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
  {
    id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
    name: "Dennis",
    url: undefined,
  },
  {
    id: "CLlPbhMULRvC2jnjnDe",
    name: "Dennis",
    url: undefined,
  },
]
console.log(users.find(user=>user.id === currentUserId))
2

1 Answer 1

1

The problem is you are using .find() instead of .some(). Try the following:

var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
  {
    id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
    name: "Dennis",
    url: undefined,
  },
  {
    id: "CLlPbhMULRvC2jnjnDe",
    name: "Dennis",
    url: undefined,
  },
]
console.log(users.some(user=>user.id === currentUserId))

The difference is in the output. .find() will return the value, .some() will return a boolean.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.