Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

ecmascript 6 - Are ES6 Array comprehensions no longer valid?

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)

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)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...