4

想要转换这个:

[["1", "2", "3"], ["4", "5", "6"]]

对此:

["1", "2", "3"], ["4", "5", "6"]

要传递给 Array.product(),第一个数组可以包含未知数量的其他数组。例如,给定的数组也可能是

[["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]]

最终,我需要将论点传递为:

otherArray.product(["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"])

提前谢谢!

4

3 回答 3

5
otherArray.product(*[["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]]);

* 在参数列表中用于将数组内容解包到参数(如此处)或将参数打包到数组中,如“def mymethod(*args)”

参考:http ://www.justskins.com/forums/apply-method-to-array-17387.html

于 2011-07-19T18:37:25.103 回答
1

I think what would work for you is using Ruby's Array expansion:

a=[[1,2,3],[4,5,6]]
b=[1,2,3].product([1,2,3],[4,5,6])
c=[1,2,3].product(*a)
b == c #This should be true

Basically putting the asterisk (*) in front of the variable will expand all elements in the array into a list of arguments, which is what you want.

于 2011-07-19T20:03:29.497 回答
0

撇开最后一行代码不谈,其余部分似乎通过使用 0 索引来解决:

arr[0]
于 2011-07-19T18:35:29.347 回答