ruby 合并 json_如何在Ruby中合并数组

ruby 合并 json

"What is the best way to combine arrays?" This question is quite vague and can mean a few different things.

“组合数组的最佳方法是什么?” 这个问题含糊不清,可能意味着不同的意思。

级联 ( Concatenation )

Concatenation is to append one thing to another. For example, concatenating the arrays [1,2,3] and [4,5,6] will give you [1,2,3,4,5,6]. This can be done in a few ways in Ruby.

串联是将一件事附加到另一件事。 例如,将数组[1,2,3][4,5,6]串联起来将得到[1,2,3,4,5,6] 。 这可以在Ruby中以几种方式完成。

The first is the plus operator. This will append one array to the end of another, creating a third array with the elements of both.

第一个是加号运算符。 这会将一个数组追加到另一个数组的末尾,从而创建包含两个元素的第三个数组。

Alternatively, use the concat method (the + operator and concat method are functionally equivalent).

或者,使用concat方法(+运算符和concat方法在功能上等效)。

If you're doing a lot of these operations you may wish to avoid this. Object creation is not free, and every one of these operations creates a third array. If you want to modify an array in place, making it longer with new elements you can use the << operator. However, if you try something like this, you'll get an unexpected result.

如果您要执行许多此类操作,则可能希望避免这种情况。 创建对象不是免费的,并且这些操作中的每一个都会创建第三个数组。 如果要就地修改数组,使用新元素使其更长,可以使用<<运算符。 但是,如果尝试这样的操作,将会得到意想不到的结果。

Instead of the expected [1,2,3,4,5,6] array we get [1,2,3,[4,5,6]]. This makes sense, the append operator takes the object you give it and appends it to the end of the array. It didn't know or care that you tried to append another array to the array. So we can loop over it ourselves.

而不是预期的[1,2,3,4,5,6]数组,我们得到[1,2,3,[4,5,6]] 。 这是有道理的,append运算符采用您提供的对象并将其附加到数组的末尾。 它不知道或不在乎您是否尝试将另一个数组追加到该数组。 这样我们就可以自己遍历它。

设定作业 ( Set Operations )

The world "combine" can also be used to describe the set operations. The basic set operations of intersection, union, and difference are available in Ruby. Remember that "sets" describe a set of objects (or in mathematics, numbers) that are unique in that set. For example, if you were to do a set operation on the array [1,1,2,3] Ruby will filter out that second 1, even though 1 may be in the resulting set. So be aware that these set operations are different than list operations. Sets and lists are fundamentally different things.

世界“组合”也可以用来描述设定的操作。 Ruby提供了相交,并集和差异的基本集合操作。 请记住,“集合”描述的是在该集合中唯一的一组对象(或在数学中为数字)。 例如,如果要对数组[1,1,2,3]进行设置操作,即使结果集中可能有1,Ruby也会过滤掉第二个1。 因此请注意,这些设置操作与列表操作不同。 集和列表是根本不同的东西。

You can take the union of two sets using the | operator. This is the "or" operator, if an element is in one set or the other, it's in the resulting set. So the result of [1,2,3] | [3,4,5] is [1,2,3,4,5] (remember that even though there are two threes, this is a set operation, not a list operation).

您可以使用|组合两个集合 操作员。 这是“或”运算符,如果一个元素在一个或另一个集合中,则它在结果集中。 所以[1,2,3]的结果| [3,4,5][1,2,3,4,5] (请记住,即使有两个三,这是一个设置操作,而不是列表操作)。

The intersection of two sets is another way to combine two sets. Instead of an "or" operation, the intersection of two sets is an "and" operation. The elements of the resultant set are those in both sets. And, being an "and" operation, we use the & operator. So the result of [1,2,3] & [3,4,5] is simply [3].

两组的交集是组合两组的另一种方法。 两组的交集不是“或”运算,而是“和”运算。 结果集的元素是两个集合中的元素。 并且,作为“与”运算,我们使用&运算符。 因此[1,2,3]&[3,4,5]的结果就是[3]

Finally, another way to "combine" two sets is to take their difference. The difference of two sets is the set of all objects in the first set that is not in the second set. So [1,2,3] - [3,4,5] is [1,2].

最后,“组合”两个集合的另一种方法是采用它们的区别。 两组的区别是第一组中所有对象的集合而不是第二组中的所有对象。 所以[1,2,3]-[3,4,5][1,2]

压缩 ( Zipping )

Finally, there is "zipping." Two arrays can be zipped together combining them in a rather unique way. It's best to just show it first, and explain after. The result of [1,2,3].zip([3,4,5]) is [ [1,3], [2,4], [3,5] ]. So what happened here? The two arrays were combined, the first element being a list of all elements in the first position of both arrays. Zipping is a bit of a strange operation and you may not find much use for it. Its purpose is to combine two arrays whose elements closely correlate.

最后是“压缩”。 可以将两个数组压缩在一起,以一种非常独特的方式将它们组合在一起。 最好先显示它,然后再解释。 [1,2,3] .zip([3,4,5])的结果[[1,3],[2,4],[3,5]] 。 那这里怎么了 将两个数组组合在一起,第一个元素是两个数组的第一个位置中所有元素的列表。 压缩是一个奇怪的操作,您可能找不到太多用处。 其目的是合并两个元素紧密相关的数组。

翻译自: https://www.thoughtco.com/combining-arrays-in-ruby-2907842

ruby 合并 json

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值