dry up array comprehension for single block

This commit is contained in:
Sebastian McKenzie
2014-09-29 16:27:35 +10:00
parent 74a661bf44
commit 908beea515
12 changed files with 75 additions and 34 deletions

View File

@@ -1,12 +0,0 @@
var seattlers = function () {
var _arr = [];
customers.forEach(function (c) {
if (c.city == "Seattle") {
_arr.push({
name: c.name,
age: c.age
});
}
});
return _arr;
}();

View File

@@ -0,0 +1 @@
var seattlers = [for (customers of countries) for (c of customers) if (c.city == "Seattle") { name: c.name, age: c.age }];

View File

@@ -0,0 +1,14 @@
var seattlers = function () {
var _arr = [];
countries.forEach(function (customers) {
customers.forEach(function (c) {
if (c.city == "Seattle") {
_arr.push({
name: c.name,
age: c.age
});
}
});
});
return _arr;
}();

View File

@@ -0,0 +1,8 @@
var seattlers = customers.filter(function (c) {
return c.city == "Seattle";
}).map(function (c) {
return {
name: c.name,
age: c.age
};
});

View File

@@ -1,9 +1,3 @@
var arr = (function () {
var _arr = [];
[1, 2, 3].forEach(function (i) {
_arr.push(i * i);
});
return _arr;
})();
var arr = [1, 2, 3].map(function (i) {
return i * i;
});