Javascript:我无法使用数组索引

时间:2018-01-02 16:32:10

标签: javascript json firebase firebase-realtime-database

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

enter image description here

scores是从firebase数据库中获取的JSON对象数组。

但是,我无法访问数组中的对象。

我认为scores[1]会打印出来。

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

但它会打印undefined

1 个答案:

答案 0 :(得分:2)

不,它不会打印它,因为scores是一个JavaScript对象,而不是一个数组。另外,该对象中没有键1

要获取密钥-L1qn0mBwpny-7FVzlCF的基础对象,您需要像这样访问它:

scores['-L1qn0mBwpny-7FVzlCF'] // { "Name": "Josh", "Score": 9 }