Shamoon
Shamoon

Reputation: 43559

How can I set multiple object properties simultaneously with CoffeeScript?

      dbLocation[latitude] = data[1]
      dbLocation[longitude] = data[2]
      dbLocation[locationText] = locationText

That's my CoffeeScript, any way to optimize it so it's more condensed?

Upvotes: 2

Views: 2133

Answers (2)

Tobias P.
Tobias P.

Reputation: 4664

Here's a one-liner, but it's not really much more readable:

[dbLocation.latitude, dbLocation.longitude, dbLocation.locationText] = [data[1], data[2], locationText]

Upvotes: 1

Trevor Burnham
Trevor Burnham

Reputation: 77416

You can write

obj = {
  latitude: data[1]
  longitude: data[2]
  locationText
}

and then merge that new object in to dbLocation by writing

dbLocation[key] = val for key, val of obj

or using a function like jQuery or Underscore's extend.

Upvotes: 3

Related Questions