0
database.ref().once("value", function (snap) {
    scores = snap.val();
    console.log(scores);
    console.log(scores[1]);
});

enter image description here

scores is an array of JSON objects taken from a firebase database.

However, I cannot access the objects inside the array.

I assume that scores[1] would print.

-L1qn0mBwpny-7FVzlCF : {Name: "Josh", Score: 9}

But it prints undefined.

1
  • 4
    is it not array, this is JSON object. you can access it via scores['-L1qn0mBwpny-7FVzlCF'] Commented Jan 2, 2018 at 16:34

1 Answer 1

2

No, it won't print it because scores is a JavaScript object, and not an array. Plus, there is no key 1 in that object.

To get the underlying object of key -L1qn0mBwpny-7FVzlCF, you need to access it like this:

scores['-L1qn0mBwpny-7FVzlCF'] // { "Name": "Josh", "Score": 9 }
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way I can iterate through this like an array. The keys are auto generated so I don't know about them?
Yes of course! Please read about Object.keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.