串联阵列中的CoffeeScript数组 [英] Concatenating an array of arrays in Coffeescript
本文介绍了串联阵列中的CoffeeScript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到的CoffeeScript一种优雅的方式合并数组的数组,所以[[1,2,3],[4,5,6],[7,8,9]] == > [1,2,3,4,5,6,7,8,9]
I'm trying to find an elegant way in Coffeescript to merge an array of arrays, so that [[1,2,3],[4,5,6],[7,8,9]] ==> [1,2,3,4,5,6,7,8,9].
正如你可能想象,我需要这个,因为我是从一个函数在构建并需要连接所产生的嵌套数组的数组产生了:
As you might imagine, I need this because I'm generating arrays from a function in a "for in" construct and need to concatenate the resulting nested array:
结果=(generate_array(X)在ARR x)
result = (generate_array(x) for x in arr)
有没有办法解决这个优雅的方式?感谢您的指点!
Is there an elegant way to handle this? Thanks for any pointers!
推荐答案
只需使用JS成语:
[].concat.apply([], a)
这在咖啡变得更好一点:
which becomes a little nicer in Coffee:
$ coffee -e 'a = [[1,2,3],[4,5,6],[7,8,9]]; console.dir [].concat a...'
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
这篇关于串联阵列中的CoffeeScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文