Reputation: 5192
This may be simple question, but i tried a lot and i can't find the solution. My problem is i want to loop through an object and get the properties of the object.
My object look like this:
{ value1: '0.92',
value2: '3728104',
value3: '43',
value4: '0.66',
}
I want to get value1, value2, value3, value4
My code:
console.log("bbbbb=" +util.inspect(results));
for (var prop in results) {
console.log("Inside for");----------------------> This is printing once.
keys.push(prop);
console.log("After push");--------------------->This is not printed..
}
console.log("keys=" +keys)
But its not looping ,, Help me to solve this.. Thanks in advance..
Upvotes: 1
Views: 55
Reputation: 239473
You can use Object.keys
function like this
console.log(Object.keys(results));
# [ 'value1', 'value2', 'value3', 'value4' ]
Actually, your code has no problems at all
var results = {
value1: '0.92',
value2: '3728104',
value3: '43',
value4: '0.66',
};
var keys = [];
for (var prop in results) {
keys.push(prop);
}
console.log(keys);
# [ 'value1', 'value2', 'value3', 'value4' ]
Upvotes: 3