问题描述
下面的ES6代码段无效。以前是有效的。我仍然可以在旧版本的Traceur中运行它,但最新的Babel和Traceur似乎不再像阵列中的for-loop那样喜欢。任何人都可以告诉我为什么它不再有效。
The ES6 code snippet below is invalid. It used to be valid. I can still run it in old versions of Traceur but the latest Babel and Traceur don't seem to like the for-loop in an array anymore. Can anyone tell me why it's no longer valid.
let people = [
{
"firstName": "Belinda",
"phone": "1-607-194-5530",
"email": "dignsissim.Maecenas.ornare@lacusAliquam.co.uk"
},
{
"firstName": "Elizabeth",
"phone": "1-155-446-1624",
"email": "cursus.et.magna@nislsemconsequat.edu"
}
]
let phones = [for({phone} of people) phone];
console.log(phones)
下面的代码片段是有效的ES6,所以我知道破坏里面一个for循环是OK
The snippet below is valid ES6 so I know the destructing inside a for-loop is OK
for(let {phone} of people) {
console.log(phone)
}
推荐答案
数组解析是 。 没有提及理解,所以可能会被删除。通过ES的快速搜索讨论邮件列表档案在任何明确的方面都是空的。
Array comprehensions were removed in BabelJS version 6. The ES2015 Specification has no mention of comprehensions, so they were probably dropped. A quick search through the ES Discuss mailing list archives came up empty on anything definitive.
作为稍微更详细的替代方案,有(ES7中的功能)和 Array.prototype.map
。
As a slightly more verbose alternative there is Object.entries
(a stage-3 feature in ES7) and Array.prototype.map
.
let emails = people.map(({ email }) => email);
这篇关于ES6阵列理解是否不再有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!