Isma Haro
Isma Haro

Reputation: 263

Issue on for each in javascript through an array of objects

I have this code:

    for(i = 0; i < data.length ; i++){
        for(var prop in data[i]){
            if (data[i].hasOwnProperty(prop)) {
                element = fn.createActivityElement(prop,data[i].prop);
                $tableData.append(element);
            }
        }
    }

Where data is this this object on a .json file:

data = [
{
    "solved": false,
    "workStation": "",
    "procedure": "OP. 50 ATORNILLADO DE FRAME Y ENSAMBLE DE ARNES",
    "operation": "50",
    "machine": "",
    "partNumber": "738",
    "client": "VW",
    "cell": "A7",
    "activity": "Atornillado de damper a riel",
    "activityNumber": "1",
    "type": "POKA YOKE",
    "description": "2 tornillos \n 3.5 Nm +/- 0.35",
    "color" : {
        "AK1" : "#C9C9C9",
        "ZB6" : "#EFEB86",
        "DM4" : "#000000"
    }
}....

My for loop goes through each element(which are objects) of my array data But then on the second loop for (var prop in data[i]) I have debuged and the issue is when I try to access:

data[i].prop -> this give me UNDEFINED



Is weird because on data[i].hasOwnProperty(prop) it gives true.
Help! please

Upvotes: 0

Views: 34

Answers (1)

user2182349
user2182349

Reputation: 9782

data[i].prop refers to the property named prop.

data[i][prop] refers to the property named by the contents of the variable prop.

Upvotes: 5

Related Questions