implement array comprehension

This commit is contained in:
Sebastian McKenzie
2014-09-29 15:35:03 +10:00
parent b0cfbb20ca
commit 74a661bf44
14 changed files with 112 additions and 2 deletions

View File

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

View File

@@ -0,0 +1,12 @@
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 arr = [for (x of "abcdefgh".split("")) for (y of "12345678".split("")) (x + y)];

View File

@@ -0,0 +1,11 @@
var arr = (function () {
var _arr = [];
"abcdefgh".split("").forEach(function (x) {
"12345678".split("").forEach(function (y) {
_arr.push(x + y);
});
});
return _arr;
})();

View File

@@ -0,0 +1 @@
var arr = [for (i in [1, 2, 3]) i * i];

View File

@@ -0,0 +1,3 @@
{
"throws": "for-in array comprehension is not supported"
}

View File

@@ -0,0 +1 @@
var arr = [for (i of [1, 2, 3]) i * i];

View File

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