Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Arrays 如何在ruby中遍历对象数组_Arrays_Ruby_Object - Fatal编程技术网

Arrays 如何在ruby中遍历对象数组

Arrays 如何在ruby中遍历对象数组,arrays,ruby,object,Arrays,Ruby,Object,我这里有一个数组,我需要得到每个对象的id [{ id: 1, points: 60 }, { id: 2, points: 20 }, { id: 3, points: 95 }, { id: 4, points: 75 }] customers = [{ id: 1, points: 90 }, { id: 2, points: 20 }, { id: 3, points: 70 }, { id: 4, points: 40 }, { id: 5, points: 60 },

我这里有一个数组,我需要得到每个对象的id

[{ id: 1, points: 60 }, { id: 2, points: 20 }, { id: 3, points: 95 }, { id: 4, points: 75 }]
        customers = [{ id: 1, points: 90 }, { id: 2, points: 20 }, { id: 3, points: 70 }, { id: 4, points: 40 }, { id: 5, points: 60 }, { id: 6, points: 10}]
我知道如何用你的手穿过整个阵列

@scores.each_with_index{ |score, index| }
但是,我还没有找到一种方法来获取对象的点。

您所谓的分数实际上是一个类似{id:1,points:60}的散列,我将其称为item

那么,让我们试试看

@scores.each_with_index do |item, index|
  puts "#{index + 1}: id #{item[:id]}, points #{item[:points]}"
end
你所谓的分数实际上是一个散列,比如{id:1,points:60},我将它称为item

那么,让我们试试看

@scores.each_with_index do |item, index|
  puts "#{index + 1}: id #{item[:id]}, points #{item[:points]}"
end
这里有一个数组,我需要得到每个对象的id

[{ id: 1, points: 60 }, { id: 2, points: 20 }, { id: 3, points: 95 }, { id: 4, points: 75 }]
        customers = [{ id: 1, points: 90 }, { id: 2, points: 20 }, { id: 3, points: 70 }, { id: 4, points: 40 }, { id: 5, points: 60 }, { id: 6, points: 10}]
为了转换集合的每个元素,您可以更精确地使用或:

customers.map{u 1[:id]} => [1, 2, 3, 4, 5, 6] 这里有一个数组,我需要得到每个对象的id

[{ id: 1, points: 60 }, { id: 2, points: 20 }, { id: 3, points: 95 }, { id: 4, points: 75 }]
        customers = [{ id: 1, points: 90 }, { id: 2, points: 20 }, { id: 3, points: 70 }, { id: 4, points: 40 }, { id: 5, points: 60 }, { id: 6, points: 10}]
为了转换集合的每个元素,您可以更精确地使用或:

customers.map{u 1[:id]} => [1, 2, 3, 4, 5, 6]
这个给定的构造是一个对象数组,因此我们需要逐个迭代每个元素并打印出对象中的值。下面的代码显示了如何执行此操作:

customers.each{| obj | p obj[:id]。to|s++obj[:points]。to|s}
这里,我们迭代每个元素,并使用obj[:id]/obj[:points]obj作为每个单独的对象打印出散列的各个实体。

这个给定的构造是一个对象数组,因此我们需要单独迭代每个元素,并打印出对象中的值。下面的代码显示了如何执行此操作:

customers.each{| obj | p obj[:id]。to|s++obj[:points]。to|s}
在这里,我们迭代每个元素,并使用obj[:id]/obj[:points]obj作为每个单独的对象打印出散列的各个实体。

这样的东西怎么样

customers.map(&:to_proc).map{ |p| [:id, :points].map(&p) }
=> [[1, 90], [2, 20], [3, 70], [4, 40], [5, 60], [6, 10]]

像这样的怎么样

customers.map(&:to_proc).map{ |p| [:id, :points].map(&p) }
=> [[1, 90], [2, 20], [3, 70], [4, 40], [5, 60], [6, 10]]

也许你正在寻找以下信息

customers = [
  { id: 1, points: 90 }, { id: 2, points: 20 },
  { id: 3, points: 70 }, { id: 4, points: 40 },
  { id: 5, points: 60 }, { id: 6, points: 10}
]
h.keys
  #=> [1, 2, 3, 4, 5, 6] 
h.values
  #=> [90, 20, 70, 40, 60, 10] 
h[2]
  #=> 20
h.key?(5)
  #=> true 
h.key?(7)
  #=> false 
h.value?(70)
  #=> true 
h.value?(30)
  #=> false 
这允许您轻松提取感兴趣的信息,如以下内容

customers = [
  { id: 1, points: 90 }, { id: 2, points: 20 },
  { id: 3, points: 70 }, { id: 4, points: 40 },
  { id: 5, points: 60 }, { id: 6, points: 10}
]
h.keys
  #=> [1, 2, 3, 4, 5, 6] 
h.values
  #=> [90, 20, 70, 40, 60, 10] 
h[2]
  #=> 20
h.key?(5)
  #=> true 
h.key?(7)
  #=> false 
h.value?(70)
  #=> true 
h.value?(30)
  #=> false 

也许你正在寻找以下信息

customers = [
  { id: 1, points: 90 }, { id: 2, points: 20 },
  { id: 3, points: 70 }, { id: 4, points: 40 },
  { id: 5, points: 60 }, { id: 6, points: 10}
]
h.keys
  #=> [1, 2, 3, 4, 5, 6] 
h.values
  #=> [90, 20, 70, 40, 60, 10] 
h[2]
  #=> 20
h.key?(5)
  #=> true 
h.key?(7)
  #=> false 
h.value?(70)
  #=> true 
h.value?(30)
  #=> false 
这允许您轻松提取感兴趣的信息,如以下内容

customers = [
  { id: 1, points: 90 }, { id: 2, points: 20 },
  { id: 3, points: 70 }, { id: 4, points: 40 },
  { id: 5, points: 60 }, { id: 6, points: 10}
]
h.keys
  #=> [1, 2, 3, 4, 5, 6] 
h.values
  #=> [90, 20, 70, 40, 60, 10] 
h[2]
  #=> 20
h.key?(5)
  #=> true 
h.key?(7)
  #=> false 
h.value?(70)
  #=> true 
h.value?(30)
  #=> false 

Jörg忽略了Ruby v2.7中引入的内容。我想情人眼里出西施,但在我看来,这就像是用一根丑陋的棍子打出来的。这要归功于2:51。Jörg忽略了Ruby v2.7中引入的内容。我想情人眼里出西施,但在我看来,这就像是用一根丑陋的棍子打出来的。记在2点51分。