antonpug
antonpug

Reputation: 14296

What does an array literal with the `for` keyword inside mean in JavaScript?

var names = [name for(name in generateNames(product))];

generateNames just returns an array of column names for various properties in a product.

But what is the whole name for name in thing?

Upvotes: 1

Views: 184

Answers (2)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

That's an array comprehension.

It's a new feature of Javascript 1.7, and works like Python's list comprehensions.

Upvotes: 7

Ray Toal
Ray Toal

Reputation: 88378

It is an array comprehension. It was added to JavaScript 1.7. Works only in Mozilla browsers like Firefox AFAIK.

See https://developer.mozilla.org/en/New_in_JavaScript_1.7#Array_comprehensions_(Merge_into_Array_comprehensions)

Here is a jsfiddle you can try, in Firefox only: http://jsfiddle.net/hfARW/1/

Upvotes: 4

Related Questions