合并和交织在Ruby中两个数组 [英] Merge and interleave two arrays in Ruby

查看:198
本文介绍了合并和交织在Ruby中两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

a = ["Cat", "Dog", "Mouse"]
s = ["and", "&"]

我要数组合并取值到数组 A 这会给我:

["Cat", "and", "Dog", "&", "Mouse"]

通过Ruby的阵列和可枚举文档看,我不认为这样会做到这一点的方法。

Looking through the Ruby Array and Enumerable docs, I don't see such a method that will accomplish this.

有没有一种方法,我可以做到这一点,而无需通过每个数组迭代?

Is there a way I can do this without iterating through each array?

推荐答案

您可以做到这一点:

a.zip(s).flatten.compact

这篇关于合并和交织在Ruby中两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆