简体   繁体   English

JavaScript Obj.length 方法返回 undefined 尽管字典包含元素

[英]JavaScript Obj.length method returns undefined despite dictionary containing elements

I've been working on a project and I'm currently working on a function which dynamically creates a dictionary data structure.我一直在做一个项目,我目前正在研究一个动态创建字典数据结构的 function。 The key used is the roll number of the student, and the value is the student's name.使用的键是学生的卷号,值是学生的姓名。

But I'm unable to iterate through this dictionary.但我无法遍历这本字典。 I've tried displaying the dictionary on console, but all I could see is an empty dictionary .我试过在控制台上显示字典,但我只能看到一个空字典 I could see the elements in it, only if I expand it further .只有进一步扩展它,我才能看到其中的元素 And when I display length using Obj.length on console, it displays 'undefined'.当我在控制台上使用Obj.length显示长度时,它显示“未定义”。
I've read on other questions that Obj.length only works on arrays(ie, enumerable types), and I've tried using an array instead of a dictionary.我已经阅读了Obj.length仅适用于数组(即可枚举类型)的其他问题,并且我尝试使用数组而不是字典。 In that case, it shows an empty array and would not show values unless I manually expand it.在这种情况下,它会显示一个空数组并且不会显示值,除非我手动展开它。 I've also tried Obj.keys() method on the dictionary, and I've encountered the same issue.我也在字典上尝试过Obj.keys()方法,我也遇到了同样的问题。
This is the function's code:这是函数的代码:

function dictGenerator(rollnos, selectedValue) {
            var dict = {};
            for(let i = 0; i < rollnos.length; i++) {
                get(child(dbref, "RegisterNos/" + rollnos[i])).then((snapshot)=>{
                    if(Object.keys(snapshot.val()).length-1 == selectedValue){
                        dict[rollnos[i]] = snapshot.val()["name"];
                    }
                });
            }
            console.log(dict);
            console.log(dict.length);
        }
}

Any help on how I could iterate through my dictionary would be appreciated, Thank you.任何关于我如何遍历我的字典的帮助将不胜感激,谢谢。

Edit: code implementation using promises.编辑:使用承诺的代码实现。

function dictGenerator(regnos, selectedValue) {
            const get_dict = async () => {
                var dict = {};
                for(let i = 0; i < regnos.length; i++){
                get(child(dbref, "RegisterNos/" + regnos[i])).then((snapshot)=>{
                    if(Object.keys(snapshot.val()).length-1 == selectedValue){
                        dict[regnos[i]] = snapshot.val()["name"];
                    }
                });
            }
            return dict;
            };

            get_dict().then((dict) => {
                console.log(dict);
            });
        }

Basing on comments made by VALZ and JaredSmith, this is the working code:根据 VALZ 和 JaredSmith 的评论,这是工作代码:

function dictGenerator(regnos, selectedValue) {
            const get_dict = async () => {
                var dict = {};
                for(let i = 0; i < regnos.length; i++){
                await get(child(dbref, "RegisterNos/" + regnos[i])).then((snapshot)=>{
                    if(Object.keys(snapshot.val()).length-1 == selectedValue){
                        dict[regnos[i]] = snapshot.val()["name"];
                    }
                });
            }
            return dict;
            };

            get_dict().then((dict) => {
                console.log(dict);
            });
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM