2

If i have an array of hashes, whats the best way to iterate?

var a = [{"a": "1"}, {"b": "2"}, {"c": "3"}]

for(var i in a) {
  console.log(a[i]) //prints each hash
  console.log(i)    //prints the index
}

If i want to get a,b,c or 1,2,3 whats the best way?

Thanks

14
  • 4
    You should never iterate an array with for (var i in a) in Javascript because that iterates all enumerable properties of a which can include more items than just array elements. Commented Oct 24, 2014 at 17:06
  • 1
    Fix your data structure. It should probably be: var a = {"a": "1", "b": "2", "c": "3"} and then you can just get the keys with Object.keys(a) and then get any value with a[key]. Commented Oct 24, 2014 at 17:19
  • 2
    @AxelAmthor: [{a: "1"}, {b: "2"}, {c: "3"}] is very valid JavaScript. The keys in an object literal can be identifier names, strings or numbers. Commented Oct 24, 2014 at 17:27
  • 1
    @AxelAmthor: Oh, you are confusing JSON with JavaScript object literals! They look similar but have different syntax (for example, JSON doesn't have any syntax for functions). Let me refer you to the JavaScript specification. As you can see, a PropertyName can either be a IdentifierName (e.g. foo), a StringLiteral (e.g. 'foo' or "foo") or a NumericLiteral (e.g. 42). Commented Oct 25, 2014 at 18:07
  • 1
    @AxelAmthor: { unknownprop: "value" } would be invalid JSON, but is a valid JS object literal. And yes, in this context, "works" means "valid" for me, because "invalid" JavaScript would throw a syntax error. Commented Oct 25, 2014 at 18:09

2 Answers 2

5

Something like this:

var a = [{
    "a": "1"
}, {
    "b": "2"
}, {
    "c": "3"
}]
var keys = [];
var values = [];

for (var i = 0; i < a.length; i++) {
    for (var key in a[i]) {
        if (a[i].hasOwnProperty(key)) {
            keys.push(key);
            values.push(a[i][key]);
        }
    }
}

console.log(keys);
console.log(values);
<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"></script>

It will iterate through your array and for each object, iterate through it's keys and push the keys and values into separate arrays.

Sign up to request clarification or add additional context in comments.

Comments

2

There's the for-of loop proposed in ECMAScript 6.

for (obj of a){
    var keys = Object.keys(obj) // if you don't know the keys
    var value = obj[keys[0]] //in case your object has only one key
}

This feature appeared in Firefox 31 and it is shipped with Chrome 38. IE doesn't implement it. Don't know about other browsers.

Alternatively,

a.forEach(function(obj){
    var keys = Object.keys(obj) // if you don't know the keys
    var value = obj[keys[0]] //in case your object has only one key
});

1 Comment

So for use in IE, is there another way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.