SAA SAA - 5 months ago 12
Javascript Question

i'm trying to get next object from Json Array without using member name

I'm trying to get the next object in Json array without mentioning member name get the key value name using Javascript

var Commandjson = {
"commands": {
"MyObj.SubObj.Action.ActionEvent": {
"Id": "MyObj.SubObj.Action.ActionEvent.CustomAction",
"EnableRules": ["MyObj.SelectionCountExactlyOne"],
"Actions": [{
"ActionType": "3",
"Attributes": {
"FunctionName": "MyObj.Command.close"
},
"Parameters": [{
"Name": "",
"ParameterName": "",
"ParameterType": "21",
"Value": "Selected"
}]
}]
}
}
};


From the above,
I'm trying to get the
Attributes
and
Parameters


i tried like

Commandjson.commands[0].Actions[0].Attributes.FunctionName


But getting error as

Uncaught TypeError: Cannot read property 'Actions' of undefined(…)


please help me what i missed.

Answer

You better use

Commandjson.commands['MyObj.SubObj.Action.ActionEvent'].Actions[0].Attributes.FunctionName

because commands is not an array, but an object or use the keys of the object for a dynamic access.

var Commandjson = {
    "commands": {
        "MyObj.SubObj.Action.ActionEvent": {
            "Id": "MyObj.SubObj.Action.ActionEvent.CustomAction",
            "EnableRules": ["MyObj.SelectionCountExactlyOne"],
            "Actions": [{
                "ActionType": "3",
                "Attributes": {
                    "FunctionName": "MyObj.Command.close"
                },
                "Parameters": [{
                    "Name": "",
                    "ParameterName": "",
                    "ParameterType": "21",
                    "Value": "Selected"
                }]
            }]
        }
    }
};

var commands = Object.keys(Commandjson.commands); // get all commands

console.log(Commandjson.commands[commands[0]].Actions[0].Attributes.FunctionName);

Comments