Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all values of a Javascript Object by its keys?

I have a Javascript object with some keys and values:

var obj = { 
    "key1" : "val1", 
    "key2" : "val2", 
    "key3" : "val3", 
    "key4" : ""
}

I want to iterate all keys and retrieving all values.

I tried 2 ways:

1) Using for(var key in keys)

var keys = Object.keys(obj);
for (var key in keys) {
    // ...
}

The problem with this solution is that keys object is an array, so I have to use obj[keys[key]]]. Not very pretty.

Furthermore, inspecting "key4", the return value is "0" instead of "" (empty).

2) Using forEach

Object.keys(obj).forEach(function(key){
    // ...
});

The problem in this case is that if I try to do:

Object.keys(obj).forEach(function(key){
    obj[key];  // <- obj is undefined !!
});

The "obj" variable is undefined in the foreach!

What's the best way to iterate in all keys for retrieving all values?

Thanks

like image 303
Alessandro C Avatar asked Apr 27 '16 08:04

Alessandro C


People also ask

How do I get all the values of an object?

values() Method: The Object. values() method is used to return an array of the object's own enumerable property values. The array can be looped using a for-loop to get all the values of the object.

How do I iterate over an object key?

You have to pass the object you want to iterate, and the JavaScript Object. keys() method will return an array comprising all keys or property names. Then, you can iterate through that array and fetch the value of each property utilizing an array looping method such as the JavaScript forEach() loop.

How do I get a list of keys from an object?

For getting all of the keys of an Object you can use Object. keys() . Object. keys() takes an object as an argument and returns an array of all the keys.


2 Answers

Just use for in.

var obj = { 
  "key1" : "val1", 
  "key2" : "val2", 
  "key3" : "val3", 
  "key4" : ""
};

for (var key in obj) {

  console.log(key); // key1 and etc...
  console.log(obj[key]); // val1 and etc...
}
like image 112
Ali Mamedov Avatar answered Oct 23 '22 02:10

Ali Mamedov


In your first example, you're using for-in to loop through an array, which is not generally a good idea. To loop through arrays, this answer has lots of options for you.

If you want to loop through all of an object's keys, use for-in on the object:

for (var key in obj) {
    var value = obj[key];
    // ...
}

Note that you'll see both the object's own properties and also ones it inherits from its prototype. If you just want own properties, you could add a guard:

for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        var value = obj[key];
        // ...
    }
}

...or use Object.keys (introduced in ES5, in 2009, so not on IE8; there's a shim if you need to support obsolete browsers like IE8):

Object.keys(obj).forEach(function(key) {
    var value = obj[key];
    // ...
});

In both cases, you'll only get the object's enumerable properties. If you need to include non-enumerable properties, reach for getOwnPropertyNames:

Object.getOwnPropertyNames(obj).forEach(function(key) {
    var value = obj[key];
    // ...
});

That will get the object's own (not inherited) property names, even if non-enumerable, as long as they're strings (which they always are in ES5 and earlier; in ES2015 and later, they're normally strings but can also be Symbol instances if you use Symbols).

getOwnPropertyNames was also introduced in ES5, but I don't think it can be shimmed, so you can't use it in IE8 or other obsolete browsers.

like image 32
T.J. Crowder Avatar answered Oct 23 '22 04:10

T.J. Crowder