9

Is there anything similar to pythons list comprehension for nodejs/javascript? If there is not then is it possible to make a function that has similar behavior for example

# Example 1

list_one = [[1, 2], [3, 4], [5, 6], [7, 8]]
someOfList = sum(x[1] for x in list_one)
print(someOfList) # prints 20

# Example 2
combined = "".join([str( ( int(x) + int(y) ) % 10) for x, y in zip("9999", "3333")])
print(combined) # prints 2222

Etc? Or would you have to make functions for each comprehension like behavior? I know you can make functions for each of those, but if you use a lot of list comprehensions code can get long

user3234209
  • 935
  • 2
  • 8
  • 14
  • This question was asked a few months ago [here](http://stackoverflow.com/questions/14511954/array-comprehensions-in-nodejs) but as things move pretty fast, I guess this question is not a duplicate. – Ray Toal Jan 27 '14 at 03:27

2 Answers2

14

List comprehensions put into a language's syntax what would normally be done with map and filter.

So given a Python list comprehension, you can also use map and filter:

# Python - preferred way
squares_of_odds = [x * x for x in a if x % 2 == 1]

# Python - alternate way
map(lambda x: x * x, filter(lambda x: x % 2 == 1, a))

although comprehensions are preferred in Python. JavaScript has map and filter so you can use those now.

// JavaScript
a.map(function(x){return x*x}).filter(function(x){return x%2 == 1})

The upcoming version of JavaScript will have array comprehensions in the language:

[ x*x for (x of a) if (x % 2 === 1) ]

To see what is available now in the upcoming version, see this compatibility table. As of this writing, you can see they are available in Firefox.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Can you test this array comprehension in NodeJS? I tried in Node but `var myArray = ["one", 2, 3.0, [4, 5, "six"]]; [x for (x of myArray)]` SyntaxError: Unexpected token for. I could not get it working in Chrome console as well. – hhh Sep 19 '18 at 20:29
  • 1
    Oh sorry this is a 4-1/2 year old answer, and at the time I wrote it, it was looking like JS was going to get array comprehensions, but that did not happen. It is currently a non-standard feature: http://kangax.github.io/compat-table/non-standard/ that is not supported in Node, nor anywhere else really! Things are fluid, it would seem. :) – Ray Toal Sep 19 '18 at 22:48
0

this is why coffee script was invented, you can give in a try by copying and pasting your code here (go to "try cofeescript" tab)

this is what it just gave me:

var combined, list_one, someOfList, x, y;

list_one = [[1, 2], [3, 4], [5, 6], [7, 8]];

someOfList = sum((function() {
  var _i, _len, _results;
  _results = [];
  for (_i = 0, _len = list_one.length; _i < _len; _i++) {
    x = list_one[_i];
    _results.push(x[1]);
  }
  return _results;
})());

print(someOfList);

combined = "".join([
  (function() {
    var _i, _len, _ref, _results;
    _ref = zip("9999", "3333");
    _results = [];
    for (y = _i = 0, _len = _ref.length; _i < _len; y = ++_i) {
      x = _ref[y];
      _results.push(str((int(x) + int(y)) % 10));
    }
    return _results;
  })()
]);
Guy Gavriely
  • 11,228
  • 6
  • 27
  • 42