so i have object that have many properties, i need to get all values and property names from that object, i'm not sure what is best and easiest way to do it. I already tried few methods but didn't had any luck with it
angular.forEach(array, function(value, key){
console.log(value);
});
You can also use the Object.keys() which returns an array of the keys of the object. For instance :
var obj = { 0 : "a", 1 : "b", 2 : "c"};
console.log(Object.keys(obj)); // [0, 1, 2]
You can also use the Object.values() which returns an array of the values of an object :
var obj = { 0 : "a", 1 : "b", 2 : "c"};
console.log(Object.values(obj)); // ['a', 'b', 'c']
forEach works with arrays, for object properties you need:
for (var property in object) {
if (object.hasOwnProperty(property)) {
console.log(property, ' ', object[property]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With