What is the cleanest way to make Javascript do something like Python\'s list comprehension?
In Python if I have a list of objects whose name\'s I want to \'
This is an example of a place where Coffeescript really shines
pows = [x**2 for x in foo_arr]
list_of_names = [x.name for x in list_of_objects]
The equivalent Javascript would be:
var list_of_names, pows, x;
pows = [
(function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = foo_arr.length; _i < _len; _i++) {
x = foo_arr[_i];
_results.push(Math.pow(x, 2));
}
return _results;
})()
];
list_of_names = [
(function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = list_of_objects.length; _i < _len; _i++) {
x = list_of_objects[_i];
_results.push(x.name);
}
return _results;
})()
];