How does Object.keys work when applied to a Javascript variable? -
i have code in application this:
var msg = data.data.modelstate[object.keys(data.data.modelstate)[0]];
can please explain object.keys part of code doing , give me idea data.datamodelstate object looks like.
right understanding code not working code around not can't debug find out.
object.keys()
returns array of keys of object. in code, accessing first element of array. i.e. first key of data.data.modelstate.
this code value of first key of data.data.modelstate
.
for example
suppose
data.data.modelstate={tmpkey:"some value"} var msg = data.data.modelstate[object.keys(data.data.modelstate)[0]]; console.log(object.keys(data.data.modelstate)[0]); //will print ["tmpkey"] console.log(msg); //it print "some value";
you can access key of object using []; , here accessing first key.
var msg = data.data.modelstate[object.keys(data.data.modelstate)[0]];
this become
var msg = data.data.modelstate[["tmpkey"][0]];
and become
var msg = data.data.modelstate["tmpkey"]; //simply return value of tmpkey property.
Comments
Post a Comment