Tuesday, 27 August 2013

getting the name of a key of an object in js

getting the name of a key of an object in js

I hopefully have a very simple question but if I have the code
console.log("my args", args);
and the result that comes back is
Object { pageList1=null}
how do i get the name pageList1
it doesnt matter that this is null i just need the name pageList1..
this is what i have tried
parse: function (result, args) {
var myKey = Object.keys(args);
return result.data.myKey;
}
ok so using on of the answers i updated the code to be
parse: function (result, args) {
for (variable in args) {
return result.data.variable;
}
}
but that doesnt like just the name variable as thats not in the JSON
structure..
OK so the answer was
parse: function (result, args) {
var pageListVariable = Object.keys(args)[0];
return result.data[pageListVariable];
}
when needing to change the call of the json the . needs to be replaced
with []

No comments:

Post a Comment