在 Coffeescript 中连接数组 [英] Concatenating an array of arrays in Coffeescript

查看:23
本文介绍了在 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].

正如您可能想象的那样,我需要这个,因为我正在从for in"构造中的函数生成数组,并且需要连接生成的嵌套数组:

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:

result = (generate_array(x) for x in arr)

result = (generate_array(x) for x in arr)

有没有优雅的方法来处理这个问题?感谢您的任何指点!

Is there an elegant way to handle this? Thanks for any pointers!

推荐答案

就用JS成语吧:

 [].concat.apply([], a)

在 Coffee 中变得更好:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆