ruby 如何将多个元素添加到数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20686099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:19:50  来源:igfitidea点击:

How do I add multiple elements to an array?

arraysruby

提问by davegson

I can easily add one element to an existing array:

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

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

How would I add multipleelements to my array?

我如何将多个元素添加到我的数组中?

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

我想做类似的事情arr << [2, 3],但这会为我的数组添加一个数组#=> [1, [2, 3]]

回答by falsetru

Using +=operator:

使用+=运算符:

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

回答by davegson

Make use of .push

利用 .push

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

You can also .push()all elements of another array

您还可以.push()使用另一个数组的所有元素

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

But take notice!withoutthe *it will add the second_arrayto arr.

但是要注意!没有*它会添加second_arrayarr

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


Inferior alternative:

劣质替代品:

You could also chain the <<calls:

您还可以链接<<调用:

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

回答by Arup Rakshit

You can do also as below using Array#concat:

您还可以使用以下方法执行以下操作Array#concat

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

回答by MrYoshiji

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:

一些链接:

回答by CRABOLO

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"]

回答by fangxing

Use Array#insertcan add an array to any position:

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

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

回答by IAmNaN

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

再一个,为了建立一个包含 n 次项目的数组,您可以使用 splat(又名星号,*):

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 #sampleto generate random weighted results:

我结合使用它#sample来生成随机加权结果:

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