i would like to get the parameter name of a json object as a string
myObject = { "the name": "the value", "the other name": "the other value" }
is there a way to get "the name" or "the other name" as a result ?
In jQuery:
var myObject = { "the name": "the value", "the other name": "the other value" };
var indexes = [];
$.each(myObject,function(index,value){
indexes.push(index);
});
console.log(indexes[0]) -> the name
console.log(indexes[1]) -> the other name
In pure js:
var myObject = { "the name": "the value", "the other name": "the other value" };
var indexes = [];
for(var index in myObject){
indexes.push(index);
}
console.log(indexes[0]) -> the name
console.log(indexes[1]) -> the other name
In above you could break whenever you would like. If you need all the indexes, there is faster way to put them in array:
var myObject = { "the name": "the value", "the other name": "the other value" };
var indexes = Object.keys(myObject);
console.log(indexes[0]) -> the name
console.log(indexes[1]) -> the other name
Sure. You can use a for-in loop to get property names of the object.
for ( var prop in myObject ){
console.log("Property name is: " + prop + " and value is " + myObject[prop]);
}