在 Ruby/Rails 中合并数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29978413/
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-03 00:13:17  来源:igfitidea点击:

Merge arrays in Ruby/Rails

ruby-on-railsrubyruby-on-rails-4ormrails-activerecord

提问by Croaton

How can I merge two arrays? Something like this:

如何合并两个数组?像这样的东西:

@movie = Movie.first()
@options = Movie.order("RANDOM()").first(3).merge(@movie)

But it doesn't work.

但它不起作用。

In @optionsI need an array with four elements includes @movie.

@options我需要一个包含四个元素的数组包括@movie.

回答by Nick Veys

Like this?

像这样?

?? irb
2.2.2 :001 > [1,2,3] + [4,5,6]
 => [1, 2, 3, 4, 5, 6] 

But you don't have 2 arrays.

但是你没有 2 个数组。

You could do something like:

你可以这样做:

@movie = Movie.first()
@options = Movie.order("RANDOM()").first(3).to_a << @movie

回答by joshua.paling

There's two parts to this question:

这个问题有两个部分:

  1. How to "merge two arrays"? Just use the +method:

    [1,2,3] + [2,3,4]
    => [1, 2, 3, 2, 3, 4]
    
  2. How to do what you want? (Which as it turns out, isn't merging two arrays.) Let's first break down that problem:

    @movieis an instance of your Moviemodel, which you can verify with @movie.class.name.

    @optionsis an Array, which you can verify with @options.class.name.

    All you need to know now is how to append a new item to an array (i.e., append your @movieitem to your @optionsarray)

    You do that using the double shovel:

    @options << @movie
    

    this is essentially the same as something like:

    [1,2,3] << 4
    => [1,2,3,4]
    
  1. 如何“合并两个数组”?只需使用以下+方法:

    [1,2,3] + [2,3,4]
    => [1, 2, 3, 2, 3, 4]
    
  2. 如何做你想做的事?(事实证明,这不是合并两个数组。)让我们首先分解这个问题:

    @movie是您的Movie模型的一个实例,您可以使用@movie.class.name.

    @optionsArray,您可以使用 进行验证@options.class.name

    您现在需要知道的是如何将新项目附加到数组中(即,将您的@movie项目附加到@options数组中)

    你使用双铲来做到这一点:

    @options << @movie
    

    这基本上与以下内容相同:

    [1,2,3] << 4
    => [1,2,3,4]
    

回答by spickermann

@movieisn't an array in your example, it is just a single instance of a movie. This should solve your problem:

@movie在您的示例中不是数组,它只是电影的单个实例。这应该可以解决您的问题:

@options << @movie

回答by Ana María Martínez Gómez

To merge (make the union of) arrays:

合并(合并)数组:

[1, 2, 3].union([2, 4, 6]) #=> [1, 2, 3, 4, 6] (FROM RUBY 2.6)
[1, 2, 3] | [2, 4, 6] #=> [1, 2, 3, 4, 6]

To concat arrays:

连接数组:

[1, 2, 3].concat([2, 4, 6]) #=> [1, 2, 3, 2, 4, 6] (FROM RUBY 2.6)
[1, 2, 3] + [2, 4, 6] #=> [1, 2, 3, 2, 4, 6]

To add element to an array:

向数组添加元素:

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

But it seems that you don't have arrays, but active records. You could convert it to array with to_a, but you can also do directly:

但似乎您没有数组,而是活动记录。您可以使用 将其转换为数组to_a,但您也可以直接执行以下操作:

Movie.order("RANDOM()").first(3) + [@movie]

which returns the array you want.

它返回你想要的数组。

回答by Ayman Hussain

Well, If you have an element to merge in an array you can use <<:

好吧,如果你有一个元素要合并到一个数组中,你可以使用<<

Eg: array = ["a", "b", "c"],  element = "d"
array << element 
=> ["a", "b", "c", "d"]

Or if you have two arrays and want duplicates then make use of +=or simply +based on your requirements on mutability requirements:

或者,如果您有两个数组并想要重复,则使用+=或简单地+基于您对可变性要求的要求:

Eg: array_1 = [1, 2], array_2 = [2, 3]
array_1 += array_2
=> [1, 2, 2, 3]

Or if you have two arrays and want to neglect duplicates then make use of |=or simply |:

或者,如果您有两个数组并想忽略重复项,请使用|=或简单地使用|

Eg: array_1 = [1, 2], array_2 = [2, 3]
array_1 |= array_2
=> [1, 2, 3]