Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have javascript object array:

objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];

I want to get all values of key foo as array [ 1, 3, 5 ].

There's the obvious manual way:

function getFields(input, field) {
    var output = [];
    for (var i=0; i < input.length ; ++i)
        output.push(input[i][field]);
    return output;
}

var result = getFields(objArray, "foo"); // returns [ 1, 3, 5 ]

Is there a better way? (With a reference link, if possible.)

share|improve this question
1  
The Prototype library added a "pluck" function to the Array prototype (I think), so you could write var foos = objArray.pluck("foo");. –  Pointy Oct 25 '13 at 13:16
    
@hyde - jsperf.com/map-vs-native-for-loop - please have a look at this, hope plain looping itself an good solution –  N20084753 Oct 25 '13 at 13:21
    
@N20084753 for a fair test you should also compare the native Array.prototype.map function where it exists –  Alnitak Oct 25 '13 at 13:22
    
@Alnitak - ya you are right but I can't understand how the native Array.prototype.map is optimized than native for loop, any way we need to traverse the complete array. –  N20084753 Oct 25 '13 at 13:31
    
@N20084753 This is actually in node.js, not in browser, so jquery's map is not relevant. –  hyde Oct 25 '13 at 14:08

3 Answers 3

up vote 6 down vote accepted

Yes, but it relies on newer features of JavaScript. This means it will not work in IE8 or older.

var result = objArray.map(function(a) {return a.foo;});

Documentation

share|improve this answer
    
This solution seems to be neat and simple but is it optimized than the plain looping method. –  N20084753 Oct 25 '13 at 13:17
    
doesn't the OP want a method to get any field, not just hardcoded foo ? –  Alnitak Oct 25 '13 at 13:17
    
@Alnitak Well, I want a field, but I can hard-code it, and the function in question is basically the code to use if there were no better way. So both yours and this answer work, and now I have a toss-up to decide which one to accept :) –  hyde Oct 25 '13 at 13:32

Using Array.prototype.map:

function getFields(input, field) {
    return input.map(function(o) {
        return o[field];
    });
}

See the above link for a shim for pre-ES5 browsers.

share|improve this answer

It depends of your definition of "better".

The other answers point out the use of map, which is natural (especially for guys used to functional style) and concise. I strongly recommend using it (if you don't bother with the few IE8- IT guys). So if "better" means "more concise", "maintainable", "understandable" then yes, it's way better.

In the other hand, this beauty don't come without additional costs. I'm not a big fan of microbench, but I've put up a small test here. The result are predictable, the old ugly way seems to be faster than the map function. So if "better" means "faster", then no, stay with the old school fashion.

Again this is just a microbench and in no way advocating against the use of map, it's just my two cents :).

share|improve this answer
    
Well, this is actually server side, not in a browser, so IE8 is irrelevant. Yep, map is the obivious way to go, somehow I just failed to find it with google (I found just jquery's map, which is not relevant). –  hyde Oct 25 '13 at 14:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.