Does JavaScript support array/list comprehensions like Python?

JavascriptPython

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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]);

Solution 2 - Javascript

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

Solution 3 - Javascript

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);
    }
});

Solution 4 - Javascript

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.

Solution 5 - Javascript

You could easily achieve this behavior using an application functor.

Array.prototype.ap = function(xs) {
  return this.reduce((acc, f) => acc.concat(xs.map(f)), []) 
}


const result = [x => x +1].ap([2])
console.log(result)

Solution 6 - Javascript

JavaScript no longer supports array comprehensions.

I too was looking for the JavaScript equivalent. Mozilla Developer's Network indicates that this functionality is no longer supported. The preferred syntax is referenced in the aforementioned link.

Solution 7 - Javascript

For "completeness"-sake, here's a shorter regexp version.

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

console.log(str.replace(new RegExp(ignore.split("").join("|")), "").split(""));

EDIT: To make sure that RegExp does not "choke" on special characters, ignore can be implemented as regexp literal, instead of a string:

var str =  "1234-5";
var ignore = /[\+=-]/;
console.log(str.replace(ignore, "").split(""));

Solution 8 - Javascript

It does have a poor mans version

const string = '1234-5'

const forbidden = '-'

print([int(i) for i in str(string) if i not in forbidden])
const result = string.split('').filter(char => char !== forbidden);
console.log(result)

In JS you can only iterate over single elements in array, so no extraction of multiple entries at a time like in Python.

For this particular case you should use a RegExp to filter the string though.

Solution 9 - Javascript

You could have a look at CoffeeScript. CoffeeScript adds missing features to java-script and allows you to write cleaner, more readable code. https://coffeescript.org/#coffeescript-2

You write a .coffee file and the coffeScript-compiler compiles your coffee file into a JavaScript file. Because the translation into JavaScript happens by compiling, the script should not run any slower.

So your code would look like the following in coffee script:

string = '1234-5'

forbidden = '-'

alert(JSON.stringify(+i for i in string when i isnt forbidden))

Honestly, this is even easier to read then the python counterpart. And it compiles quickly to the fallowing JavaScript:

var forbidden, i, string;

string = '1234-5';

forbidden = '-';

alert(JSON.stringify((function() {
  var j, len, results;
  results = [];
  for (j = 0, len = string.length; j < len; j++) {
    i = string[j];
    if (i !== forbidden) {
      results.push(+i);
    }
  }
  return results;
})()));

You don’t even need to install anything. On their website you can play around with it, and it will show you the translated JavaScript code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA KView Question on Stackoverflow
Solution 1 - JavascriptDRDView Answer on Stackoverflow
Solution 2 - JavascriptColin D BennettView Answer on Stackoverflow
Solution 3 - JavascriptJaromanda XView Answer on Stackoverflow
Solution 4 - JavascriptChadFView Answer on Stackoverflow
Solution 5 - JavascriptmartinView Answer on Stackoverflow
Solution 6 - JavascriptVanBantamView Answer on Stackoverflow
Solution 7 - JavascriptDRDView Answer on Stackoverflow
Solution 8 - JavascriptHenrik VendelboView Answer on Stackoverflow
Solution 9 - JavascriptDario VivaView Answer on Stackoverflow