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. So obviously I want to define function and use it.
Function takes two arguments, one is object and other one is id. Inside the function I loop through the object to get all of the data I need using object.length. Loop gets executed inside function definition and I get Uncaught TypeError: Cannot read property 'length' of undefined error in console. 
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!
 
    