简体   繁体   English

函数定义内的循环被执行

[英]Loop inside function definition gets executed

In one of my projects I'm using API to get data from other website but to get all of the data I would need to write 8 blocks of the same code just with two diffrent variables. 在我的一个项目中,我使用API​​从其他网站获取数据,但是要获取所有数据,我只需要用两个不同的变量编写8个相同代码的块。 So obviously I want to define function and use it. 所以显然我想定义函数并使用它。

Function takes two arguments, one is object and other one is id. 函数有两个参数,一个是对象,另一个是id。 Inside the function I loop through the object to get all of the data I need using object.length. 在函数内部,我循环遍历对象,以使用object.length获得所需的所有数据。 Loop gets executed inside function definition and I get Uncaught TypeError: Cannot read property 'length' of undefined error in console. 循环在函数定义内执行,并且得到Uncaught TypeError: Cannot read property 'length' of undefined在控制台中Uncaught TypeError: Cannot read property 'length' of undefined错误的Uncaught TypeError: Cannot read property 'length' of undefined

Here's my code: 这是我的代码:

function listAll(objectProp, destination) {
    for (var i = 0; objectProp.length > i; i++ ) {
        var option = document.createElement("option");
        var value = document.createTextNode(objectProp.name);
        option.appendChild(value);
        document.getElementById(destination).appendChild(option);
    }
}

I've been looking for solution but I couldn't find the same problem. 我一直在寻找解决方案,但找不到相同的问题。 Can anyone explain it to me why it's happening? 谁能向我解释为什么会这样?

Thanks! 谢谢!

Update: That's how code should look like. 更新:这就是代码的外观。

function listAll(objectProp, destination) {
    if ( typeof(objectProp) != 'undefined' ) {
        for (var i = 0; objectProp.length > i; i++ ) {
            var option = document.createElement("option");
            var value = document.createTextNode(objectProp[i].name);
            option.appendChild(value);
            document.getElementById(destination).appendChild(option);
        }
    }
}

Thanks for help! 感谢帮助!

You can't get the length of an Object in JavaScript using .length . 您无法使用.length获得JavaScript中对象的长度。 You will need to either store the data in an array or use a solution such as this one: Length of a JavaScript object 您将需要将数据存储在数组中,或者使用诸如此类的解决方案: JavaScript对象的长度

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

相关问题 在NodeJS中发送响应之前,如何确保执行“ for循环”中的函数? - How can I make sure that the function inside 'for loop' gets executed before I send the response in NodeJS? 构造函数内部对象定义内的For循环 - For loop inside object definition inside Constructor function Function 未在 Hook 中执行 - Function gets not executed in Hook 为什么我的 Promise 定义会被执行? - Why does my Promise definition gets executed? 如何仅禁用 chrome 中的“调试器”关键字,该关键字由循环内的 eval 执行? - How can I disable only the “debugger” keyword in chrome, that gets executed by an eval inside a loop? 我需要在函数内编写addEventListener的代码,并且每次选中复选框时都会执行该函数 - I need to code addEventListener inside a function and that function gets executed everytime a checkbox is checked API获取完for循环后如何调用函数? - How to call function after API gets are finished inside of for loop? 带承诺的循环后没有代码被执行 - No code gets executed after loop with promises 函数内部的函数未执行 - Function inside function is not getting executed 函数也会为每个先前的元素执行 - Function gets executed for every previous element aswell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM