Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
172
rated 0 times [  177] [ 5]  / answers: 1 / hits: 112246  / 9 Years ago, sat, july 11, 2015, 12:00:00

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?


More From » python

 Answers
32

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

[#65847] Wednesday, July 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kieraelsies

Total Points: 718
Total Questions: 103
Total Answers: 104

Location: England
Member since Sun, May 21, 2023
1 Year ago
kieraelsies questions
Tue, Aug 3, 21, 00:00, 3 Years ago
Tue, Feb 23, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Wed, Sep 9, 20, 00:00, 4 Years ago
Mon, Sep 16, 19, 00:00, 5 Years ago
;