JavaScript是否支持Python之类的数组/列表推导? [英] Does JavaScript support array/list comprehensions like Python?

查看:81
本文介绍了JavaScript是否支持Python之类的数组/列表推导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习/学习JavaScript和Python。我想知道Javascript是否与这种类型的编码等效。

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

我基本上试图从字符串中的每个单独的整数中获取一个数组用于练习目的。我比Python更精通Python

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

Javascript上面有类似的内容吗?

Does Javascript have something similar for me to do above?

推荐答案

更新:已从标准中删除了数组解析。引用 MDN

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


数组解析语法是非标准的,从Firefox 58开始删除。对于面向未来的用法,请考虑使用Array.prototype.map,Array.prototype。过滤器,箭头函数和扩展语法。

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.

原始答案:

是的,JavaScript将在即将发布的EcmaScript版本7中支持数组理解: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions

Yes, JavaScript will support array comprehensions in the upcoming EcmaScript version 7: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions.

这是一个例子:数组理解

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

console.log([for (i of str) if (!ignore.includes(i)) i]);

这篇关于JavaScript是否支持Python之类的数组/列表推导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆