Understanding Python lists nodejs / javascript

Is there something similar to understanding pythons lists for nodejs / javascript? If not, is it possible to create 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 will you need to perform functions for each understanding, such as behavior? I know that you can create functions for each of them, but if you use many methods for compiling lists, the code can take a long time.

+4
source share
2 answers

Matching lists in the language syntax, which is usually done with mapand filter.

Therefore, given the understanding of the Python list, you can also use a 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))

Python . JavaScript map filter, .

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

JavaScript :

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

, , . . , Firefox.

+6

script, , ( "try cofeescript" )

:

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;
  })()
]);
0

Source: https://habr.com/ru/post/1523832/


All Articles