admin管理员组文章数量:1025512
My application has different types of data about a fixed set of countries, kept in arrays of a consistent order.
data =
oranges: [1,2,3]
apples: [1,2,3]
cabbages: [1,2,3]
These arrays get bined by various criteria into new arrays, and I find myself wanting to write code like this:
fruit = []
for key, arr of data # For each array
if key in ['oranges', 'apples'] # It it meets certain criteria
for val, i in arr # Use the values in the creation of a new array
fruit[i] += val
This doesn't work because if fruit[i]
is not initialized +=
won't work.
There are various ways around this.
1) Fill the new fruit
array with zeros first:
for i in [0..len]
fruit[i] = 0
2) Check if fruit[i]
exists:
if fruit[i]?
fruit[i] += val
else
fruit[i] = val
Neither of these seem elegant. I tried extracting approach 2) into a function but I have to admit that I couldn't quite get my head round it. I thought about passing in fruit
, cloning it (with arr.slice(0)
) and then setting fruit
to the output, but it didn't feel right to do this on every iteration.
The data format is fixed, but other than that my question is "what is the best way of handling this?" I'm open to answers that use CoffeeScript and/or ECMAScript 5 and/or JQuery.
My application has different types of data about a fixed set of countries, kept in arrays of a consistent order.
data =
oranges: [1,2,3]
apples: [1,2,3]
cabbages: [1,2,3]
These arrays get bined by various criteria into new arrays, and I find myself wanting to write code like this:
fruit = []
for key, arr of data # For each array
if key in ['oranges', 'apples'] # It it meets certain criteria
for val, i in arr # Use the values in the creation of a new array
fruit[i] += val
This doesn't work because if fruit[i]
is not initialized +=
won't work.
There are various ways around this.
1) Fill the new fruit
array with zeros first:
for i in [0..len]
fruit[i] = 0
2) Check if fruit[i]
exists:
if fruit[i]?
fruit[i] += val
else
fruit[i] = val
Neither of these seem elegant. I tried extracting approach 2) into a function but I have to admit that I couldn't quite get my head round it. I thought about passing in fruit
, cloning it (with arr.slice(0)
) and then setting fruit
to the output, but it didn't feel right to do this on every iteration.
The data format is fixed, but other than that my question is "what is the best way of handling this?" I'm open to answers that use CoffeeScript and/or ECMAScript 5 and/or JQuery.
Share Improve this question edited Aug 22, 2013 at 8:09 Derek Hill asked Aug 22, 2013 at 7:40 Derek HillDerek Hill 6,4745 gold badges58 silver badges79 bronze badges 3-
Where is
array
defined? – elclanrs Commented Aug 22, 2013 at 7:44 -
@elclanrs sorry. Fixed typo.
array
isarr
– Derek Hill Commented Aug 22, 2013 at 8:10 -
2
fruit[i] = fruit[i] + 1 || 1
? – david Commented Aug 22, 2013 at 8:15
1 Answer
Reset to default 6You can initialize element of the array with using ||=
or ?=
operator:
fruit[i] ||= 0
fruit[i] += val
The only difference is that ?=
checks for null
or undefined
and ||=
checks for any false
value.
My application has different types of data about a fixed set of countries, kept in arrays of a consistent order.
data =
oranges: [1,2,3]
apples: [1,2,3]
cabbages: [1,2,3]
These arrays get bined by various criteria into new arrays, and I find myself wanting to write code like this:
fruit = []
for key, arr of data # For each array
if key in ['oranges', 'apples'] # It it meets certain criteria
for val, i in arr # Use the values in the creation of a new array
fruit[i] += val
This doesn't work because if fruit[i]
is not initialized +=
won't work.
There are various ways around this.
1) Fill the new fruit
array with zeros first:
for i in [0..len]
fruit[i] = 0
2) Check if fruit[i]
exists:
if fruit[i]?
fruit[i] += val
else
fruit[i] = val
Neither of these seem elegant. I tried extracting approach 2) into a function but I have to admit that I couldn't quite get my head round it. I thought about passing in fruit
, cloning it (with arr.slice(0)
) and then setting fruit
to the output, but it didn't feel right to do this on every iteration.
The data format is fixed, but other than that my question is "what is the best way of handling this?" I'm open to answers that use CoffeeScript and/or ECMAScript 5 and/or JQuery.
My application has different types of data about a fixed set of countries, kept in arrays of a consistent order.
data =
oranges: [1,2,3]
apples: [1,2,3]
cabbages: [1,2,3]
These arrays get bined by various criteria into new arrays, and I find myself wanting to write code like this:
fruit = []
for key, arr of data # For each array
if key in ['oranges', 'apples'] # It it meets certain criteria
for val, i in arr # Use the values in the creation of a new array
fruit[i] += val
This doesn't work because if fruit[i]
is not initialized +=
won't work.
There are various ways around this.
1) Fill the new fruit
array with zeros first:
for i in [0..len]
fruit[i] = 0
2) Check if fruit[i]
exists:
if fruit[i]?
fruit[i] += val
else
fruit[i] = val
Neither of these seem elegant. I tried extracting approach 2) into a function but I have to admit that I couldn't quite get my head round it. I thought about passing in fruit
, cloning it (with arr.slice(0)
) and then setting fruit
to the output, but it didn't feel right to do this on every iteration.
The data format is fixed, but other than that my question is "what is the best way of handling this?" I'm open to answers that use CoffeeScript and/or ECMAScript 5 and/or JQuery.
Share Improve this question edited Aug 22, 2013 at 8:09 Derek Hill asked Aug 22, 2013 at 7:40 Derek HillDerek Hill 6,4745 gold badges58 silver badges79 bronze badges 3-
Where is
array
defined? – elclanrs Commented Aug 22, 2013 at 7:44 -
@elclanrs sorry. Fixed typo.
array
isarr
– Derek Hill Commented Aug 22, 2013 at 8:10 -
2
fruit[i] = fruit[i] + 1 || 1
? – david Commented Aug 22, 2013 at 8:15
1 Answer
Reset to default 6You can initialize element of the array with using ||=
or ?=
operator:
fruit[i] ||= 0
fruit[i] += val
The only difference is that ?=
checks for null
or undefined
and ||=
checks for any false
value.
本文标签: javascriptCoffeeScript Initialize or increment value in arrayStack Overflow
版权声明:本文标题:javascript - CoffeeScript: Initialize or increment value in array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745635696a2160433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论