Skip to content Skip to sidebar Skip to footer

Firebase: Getting Value Of A Child Without Knowing Its Key

How can I get the value of a child element without knowing the key of that child element? Example data structure below; ->cars -->3282jasjd893j: doors -->819idid82jkdf: wi

Solution 1:

Thanks a lot for all your comments and advice. I've got the answer now, and here it is:

     let inputArray = [];
     snapshot.forEach(snap => {
     inputArray.push(snap.val());
     return false;
   });

Solution 2:

While you answered your own question it's probably not going to be a good long term solution.

For example: Suppose you want the doors node and there are 100,000 other nodes. You'll have to load all 100,000 and iterate over them to locate the node. That could exceed the capacity of the device and cause a sluggish experience for the user.

The better answer is to fix the structure:

cars
 3282jasjd893j:
     item: doors
 819idid82jkdf:
     item: windows
 fjf842jr8448r:
     item: audi

Now you can simply query the cars->item node for 'doors' and it will return the node you want - no iterating necessary!

 3282jasjd893j:
     item: doors

Post a Comment for "Firebase: Getting Value Of A Child Without Knowing Its Key"