0

I have a firestore collection which is an object that contains other objects. I want to search this larger object based on an ID. The object looks like this:

{
  id1: {name: 'a', somethingElse: 'a'},
  id2: {name: 'b', somethingElse: 'b'},
  id3: {name: 'c', somethingElse: 'c'},

}

Since it isn't an array, and I am unable to use .find(). How can I search the object using the id, and return the name?

This is what I'm working with so far:

  function findUser(id, users){
         users.find(function(id{
          return id.name
         })
        }

which, of course, doesn't work

Jack Collins
  • 339
  • 1
  • 2
  • 13
  • Possible duplicate of [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – serverSentinel May 28 '19 at 21:21

1 Answers1

4

You can directly access objects inside another object by id:

obj[id]

let store = {
  id1: {name: 'a', somethingElse: 'a'},
  id2: {name: 'b', somethingElse: 'b'},
  id3: {name: 'c', somethingElse: 'c'},
}

function findUser(id, users){
  return users[id].name;
}

console.log(findUser('id2', store));
falinsky
  • 7,229
  • 3
  • 32
  • 56