开发者

How can I set multiple object properties simultaneously with CoffeeScript?

开发者 https://www.devze.com 2023-04-05 05:43 出处:网络
dbLocation[latitude] = data[1] dbLocation[longitude] = data[2] dbLocation[locationText] = locationTex开发者_如何学Ct
      dbLocation[latitude] = data[1]
      dbLocation[longitude] = data[2]
      dbLocation[locationText] = locationTex开发者_如何学Ct

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


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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消