Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Ruby中获取数组的第n个元素?_Javascript_Ruby_Arrays - Fatal编程技术网

Javascript 在Ruby中获取数组的第n个元素?

Javascript 在Ruby中获取数组的第n个元素?,javascript,ruby,arrays,Javascript,Ruby,Arrays,我有一个简单的数组,我试图抓住数组中的每2个项目。不幸的是,我对JavaScript比对Ruby更熟悉 在JavaScript中,我可以简单地执行以下操作 var arr=[1',foo',bar',baz',9], otherArr=[]; 对于(i=0;i你可以和一起使用。 使用基于1的索引可能更容易阅读: arr.select.with_index(1) { |e, i| i.odd? } #=> [1, "bar", 9] 或查找每个第n个元素,如下所示: 每个_片都可以很好地用

我有一个简单的数组,我试图抓住数组中的每2个项目。不幸的是,我对JavaScript比对Ruby更熟悉

在JavaScript中,我可以简单地执行以下操作

var arr=[1',foo',bar',baz',9],
otherArr=[];
对于(i=0;i

现在,我怎样才能在Ruby中做到这一点呢?

同样,您也可以在Ruby中做到这一点:

arr = [1, 'foo', 'bar', 'baz', 9],
otherArr = [];

arr.each_with_index do |value,index|
  # Do something... for example:
  otherArr <<  arr[i] if i.even?
end
arr=[1',foo',bar',baz',9],
otherArr=[];
带索引do的每个数组值,索引|
#做点什么。。。例如:
奥瑟拉尔
请尝试上面的代码

arr = [1, 'foo', 'bar', 'baz', 9]
new_array = []
为了获得机会

arr.each_with_index{|x, i| new_array << x if i.odd?} 
new_array #=> ['foo', 'baz']

一个很好的方法是选择每一对,只选择其中的第一对:

arr = [1, 'foo', 'bar', 'baz', 9]
other_arr = arr.each_slice(2).map(&:first)
# => [1, "bar", 9] 

我喜欢将
每个带有索引的\u与
地图
相结合

arr = [1, 'foo', 'bar', 'baz', 9]
arr.each_with_index.map { |i,k| i if k.odd? }.compact
#=> ["foo", "baz"]
但是,您也可以使用该方法

对于奇数索引

arr.values_at(*(1..arr.size).step(2)).compact
#=> ["foo", "baz"]
对于偶数索引

arr.values_at(*(0..arr.size).step(2))
#=> [1, "bar", 9]
(我不认为这样做是明智的)——(<)/P> < P>你可以和

一起使用。 使用基于1的索引可能更容易阅读:

arr.select.with_index(1) { |e, i| i.odd? }
#=> [1, "bar", 9]
或查找每个第n个元素,如下所示:


每个_片都可以很好地用于这种情况:

arr = [1, 'foo', 'bar', 'baz', 9]
n = 2
arr.each_slice(n) { |a1, a2| p a1 }

> 1, "bar", 9

arr.each_slice(n) { |a1, a2| p a2 }

> "foo", "baz"

在Ruby中,您可以像在其他语言中一样索引数组。从索引0开始。这里有什么问题?为什么不谷歌“ruby array”并阅读关于array类的文档呢?这个问题只需要很少的研究工作,显然你没有做任何研究。请查看。要在ruby上获取数组的第n个元素,您应该执行
arr[n-1]
。但这不是你想要的,所以一旦编辑锁可用,请有人更改它。请检查我下面的答案。您可以在这里使用更多选项:它不起作用-它只是运行
。每个元素上的odd?
都是
'baz'。odd?
true?它在数组内元素的索引上调用
odd?
。我对“foo”使用了-
NoMethodError:undefined method
偶?':String`我尝试了这个方法,但它甚至找不到方法
NoMethodError:undefined method
偶数?“foo”:String`谢谢你的捕获,@UriAgassi我只是用整数运行它。它现在是固定的。不会
b=n.step(a.size,n)。map{i|a[i-1]}
更简洁吗?或者
(1..a.size)。step(n)。map{i|a[i]}
。你不需要一个块:
arr.each(2)。map(&:first)
或者
arr.select。使用{u索引(1)}e,i |a[i]}为零。对于每个项
。我更喜欢你的解决方案,因为它是最简单、可读性最好的。你不需要
来压缩
,你可以通过排除最后一个元素来省略
压缩
(因为它不适用于
nil
元素):
arr.values\u at(*(0…arr.size)。步骤(2))
arr.select.with_index { |e, i| i.even? }
#=> [1, "bar", 9]
arr.select.with_index(1) { |e, i| i.odd? }
#=> [1, "bar", 9]
n = 2
arr.select.with_index(1) { |e, i| (i % n).zero? }
#=> [1, "bar", 9]
arr = [1, 'foo', 'bar', 'baz', 9]
n = 2
arr.each_slice(n) { |a1, a2| p a1 }

> 1, "bar", 9

arr.each_slice(n) { |a1, a2| p a2 }

> "foo", "baz"