Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JavaScript support array/list comprehensions like Python?

I'm practicing/studying both JavaScript and Python. I'm wondering if Javascript has the equivalence to this type of coding.

I'm basically trying to get an array from each individual integer from the string for practice purposes. I'm more proficient in Python than JavaScript

Python:

string = '1234-5'

forbidden = '-'

print([int(i) for i in str(string) if i not in forbidden])

Does Javascript have something similar for me to do above?

like image 389
A K Avatar asked Jul 11 '15 03:07

A K


People also ask

Are there list comprehensions in JavaScript?

If you're wondering if JavaScript supports a list comprehension syntax like Python, the answer is unfortunately no.

Does typescript have list comprehension?

List comprehension is a method from mathematics to describe a collection of values in a very compact and elegant way.

Are Python list comprehensions lazy?

Python list comprehensions are not lazy, though. If you generate [for x in xrange(1, 10000)] you'll get 10000 elements in your list. My understanding is that LINQ list comprehensions are that.

Why are list comprehensions faster in Python?

List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration.


4 Answers

Update: Array comprehensions were removed from the standard. Quoting MDN:

The array comprehensions syntax is non-standard and removed starting with Firefox 58. For future-facing usages, consider using Array.prototype.map, Array.prototype.filter, arrow functions, and spread syntax.

See this answer for an example with Array.prototype.map:

let emails = people.map(({ email }) => email);

Original answer:

Yes, JavaScript will support array comprehensions in the upcoming EcmaScript version 7.

Here's an example.

var str =  "1234-5";
var ignore = "-";

console.log([for (i of str) if (!ignore.includes(i)) i]);
like image 155
DRD Avatar answered Nov 03 '22 00:11

DRD


Given the question's Python code

print([int(i) for i in str(string) if i not in forbidden])

this is the most direct translation to JavaScript (ES2015):

const string = '1234-5';
const forbidden = '-';

console.log([...string].filter(c => !forbidden.includes(c)).map(c => parseInt(c)));
// result: [ 1, 2, 3, 4, 5 ]

Here is a comparison of the Python and JavaScript code elements being used: (Python -> Javascript):

  • print -> console.log
  • iterate over characters in a string -> spread operator
  • list comprehension 'if' -> Array.filter
  • list comprehension 'for' -> Array.map
  • substr in str? -> string.includes
like image 29
Colin D Bennett Avatar answered Nov 03 '22 01:11

Colin D Bennett


Reading the code, I assume forbidden can have more than 1 character. I'm also assuming the output should be "12345"

var string = "12=34-5";

var forbidden = "=-";

console.log(string.split("").filter(function(str){
    return forbidden.indexOf(str) < 0;
}).join(""))

If the output is "1" "2" "3" "4" "5" on separate lines

var string = "12=34-5";

var forbidden = "=-";

string.split("").forEach(function(str){
    if (forbidden.indexOf(str) < 0) {
        console.log(str);
    }
});
like image 24
Jaromanda X Avatar answered Nov 03 '22 00:11

Jaromanda X


Not directly, but it's not hard to replicate.

var string = "1234-5";

var forbidden = "-";

string.split("").filter(function(str){
    if(forbidden.indexOf(str) < 0) {
        return str;
    }
}).forEach(function(letter) { console.log(letter);});

I guess more directly:

for(var i=0 ; i < str.length ; i++) {
    if(forbidden.indexOf(str) < 0) {
        console.log(str[i]);
    }
}

But there's no built in way to filter in your for loop.

like image 23
ChadF Avatar answered Nov 03 '22 02:11

ChadF