Lindenk
Lindenk

Reputation: 502

Create an array of object using a for loop in coffescript

Quick question. I can't find the syntax for it (well assuming it exists) for creating an object from a for loop. What is the correct way to do this -

letters = ['a', 'b', 'c']
objs = (letter[index]:index for letter, index in letters)

#Also tryed..
objs = ({letter[index]:index} for letter, index in letters)

Any ideas?

Upvotes: 0

Views: 460

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146064

coffee> letters = ['a', 'b', 'c']
[ 'a', 'b', 'c' ]
coffee> lettersToIndex = {}
{}
coffee> lettersToIndex[letter] = index for letter, index in letters
[ 0, 1, 2 ]
coffee> lettersToIndex
{ a: 0, b: 1, c: 2 }

Upvotes: 2

Related Questions