Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 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": "[email protected]"
    },
    {
        "firstName": "Elizabeth",
        "phone": "1-155-446-1624",
        "email": "[email protected]"
    }
]

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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...