0

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

1 Answer 1

5

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

:O you are right! But what if I want to use "point notation" ? how can I do it?
You can't use "point notation" when using a variable to store the name of the property to access. If you need finer control, you could use an if statement to test the name of prop before using it.
Okay Thanks a lot! :D !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.