How to get all values of a Javascript Object by its keys?
<p>I have a Javascript object with some keys and values:</p>

<pre><code>var obj = { 
    "key1" : "val1", 
    "key2" : "val2", 
    "key3" : "val3", 
    "key4" : ""
}
</code></pre>

<p>I want to iterate all keys and retrieving all values.</p>

<p>I tried 2 ways:</p>

<p>1) Using for(var key in keys)</p>

<pre><code>var keys = Object.keys(obj);
for (var key in keys) {
    // ...
}
</code></pre>

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

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

<p>2) Using forEach</p>

<pre><code>Object.keys(obj).forEach(function(key){
    // ...
});
</code></pre>

<p>The problem in this case is that if I try to do:</p>

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

<p>The "obj" variable is undefined in the foreach!</p>

<p>What's the best way to iterate in all keys for retrieving all values?</p>

<p>Thanks</p>