如何向数组中添加多个元素?

时间:2022-07-05 07:06:23

I can easily add one element to an existing array:

我可以轻松地向现有数组添加一个元素:

arr = [1]
arr << 2
# => [1, 2]

How would I add multiple elements to my array?

如何向数组中添加多个元素?

I'd like to do something like arr << [2, 3], although this adds an array to my array #=> [1, [2, 3]]

我想做一些类似于arr <[2, 3]的事情,尽管这会向我的数组#=>[1,[2,3]添加一个数组

7 个解决方案

#1


46  

Using += operator:

使用+ =操作符:

arr = [1]
arr += [2, 3]
arr
# => [1, 2, 3]

#2


18  

After some research I found two ways to do so:

经过一些研究,我发现了两种方法:

arr = [1]

arr.push(2, 3)
arr << 2 << 3
# => [1, 2, 3]

Both return my desired array.

都返回所需的数组。

#3


13  

You can do also as below using Array#concat:

你也可以使用数组#concat:

arr = [1]
arr.concat([2, 3]) # => [1, 2, 3]

#4


6  

There is several methods to achieve that:

有几种方法可以实现这一点:

array = [1, 2]

array += [3, 4] # => [1, 2, 3, 4]

# push: put the element at the end of the array
array.push([5, 6]) # =>  [1, 2, 3, 4, [5, 6]]
array.push(*[7, 8]) # => [1, 2, 3, 4, [5, 6], 7, 8]

array << 9 # => [1, 2, 3, 4, [5, 6], 7, 8, 9]

# unshift: put the element at the beginning of the array:
array.unshift(0) #=> [0, 1, 2, 3, 4, [5, 6], 7, 8, 9]

Some links:

一些链接:

#5


0  

just use .flatten

只使用.flatten

for example if you have this array

例如,如果你有这个数组

array = [1,2,3,4,5,6]

and you do this

和你这样做

array.push([123,456,789])
array.push([["abc","def"],["ghi","jkl"]])

your string would look something like

你的绳子看起来是这样的

array = [[1,2,3,4,5,6],[123,456,789],[["abc","def"],["ghi","jkl"]]]

all you need to do is

你所需要做的就是

array.flatten!

and now your array would like this

现在你的数组是这样的

array = [1,2,3,4,5,6,123,456,789,"abc","def","ghi","jkl"]

#6


0  

One more, for building up an array with items n-times you can use the splat (AKA asterisk, *):

另外,要用n次的项目构建一个数组,可以使用splat (AKA asterisk, *):

arr = [true] * 4           # => [true, true, true, true]

You can also use the splat for repeating multiple elements:

您也可以使用splat来重复多个元素:

arr = [123,'abc'] * 3      # => [123,'abc',123,'abc',123,'abc']

Of course, you can use any array operators with that, such as +:

当然,可以使用任何数组运算符,比如+:

arr = [true] * 3 + [false] # => [true, true, true, false]

I use that in conjunction with #sample to generate random weighted results:

我将它与#sample一起用于生成随机加权结果:

arr.sample                 # => true 3 out of 4 times

#7


0  

Use Array#insert can add an array to any position:

使用数组#insert可以将数组添加到任何位置:

a = [1, 2, 3]
b = [4,5,6]
b.insert(0, *a)
=> [1, 2, 3, 4, 5, 6]

#1


46  

Using += operator:

使用+ =操作符:

arr = [1]
arr += [2, 3]
arr
# => [1, 2, 3]

#2


18  

After some research I found two ways to do so:

经过一些研究,我发现了两种方法:

arr = [1]

arr.push(2, 3)
arr << 2 << 3
# => [1, 2, 3]

Both return my desired array.

都返回所需的数组。

#3


13  

You can do also as below using Array#concat:

你也可以使用数组#concat:

arr = [1]
arr.concat([2, 3]) # => [1, 2, 3]

#4


6  

There is several methods to achieve that:

有几种方法可以实现这一点:

array = [1, 2]

array += [3, 4] # => [1, 2, 3, 4]

# push: put the element at the end of the array
array.push([5, 6]) # =>  [1, 2, 3, 4, [5, 6]]
array.push(*[7, 8]) # => [1, 2, 3, 4, [5, 6], 7, 8]

array << 9 # => [1, 2, 3, 4, [5, 6], 7, 8, 9]

# unshift: put the element at the beginning of the array:
array.unshift(0) #=> [0, 1, 2, 3, 4, [5, 6], 7, 8, 9]

Some links:

一些链接:

#5


0  

just use .flatten

只使用.flatten

for example if you have this array

例如,如果你有这个数组

array = [1,2,3,4,5,6]

and you do this

和你这样做

array.push([123,456,789])
array.push([["abc","def"],["ghi","jkl"]])

your string would look something like

你的绳子看起来是这样的

array = [[1,2,3,4,5,6],[123,456,789],[["abc","def"],["ghi","jkl"]]]

all you need to do is

你所需要做的就是

array.flatten!

and now your array would like this

现在你的数组是这样的

array = [1,2,3,4,5,6,123,456,789,"abc","def","ghi","jkl"]

#6


0  

One more, for building up an array with items n-times you can use the splat (AKA asterisk, *):

另外,要用n次的项目构建一个数组,可以使用splat (AKA asterisk, *):

arr = [true] * 4           # => [true, true, true, true]

You can also use the splat for repeating multiple elements:

您也可以使用splat来重复多个元素:

arr = [123,'abc'] * 3      # => [123,'abc',123,'abc',123,'abc']

Of course, you can use any array operators with that, such as +:

当然,可以使用任何数组运算符,比如+:

arr = [true] * 3 + [false] # => [true, true, true, false]

I use that in conjunction with #sample to generate random weighted results:

我将它与#sample一起用于生成随机加权结果:

arr.sample                 # => true 3 out of 4 times

#7


0  

Use Array#insert can add an array to any position:

使用数组#insert可以将数组添加到任何位置:

a = [1, 2, 3]
b = [4,5,6]
b.insert(0, *a)
=> [1, 2, 3, 4, 5, 6]