Extrair valores de matriz ruby

Exemplos de código

0
0

ruby extrair elementos de matriz

Suppose you have an array of hashes like this:

```ruby
arr = [{a: 1, b: 2}, {c: 3, d: 4}, {e: 5, f: 6}]
```

You can extract the values corresponding to a given key like this:

```ruby
arr.map { |h| h[:a] }
# => [1, nil, nil]
```

```ruby
arr.map { |h| h[:b] }
# => [2, nil, nil]
```

```ruby
arr.map { |h| h[:c] }
# => [nil, 3, nil]
```

```ruby
arr.map { |h| h[:d] }
# => [nil, 4, nil]
```

```ruby
arr.map { |h| h[:e] }
# => [nil, nil, 5]
```

```ruby
arr.map { |h| h[:f] }
# => [nil, nil, 6]
```

If you want to get all the values for all the keys, you can use `#values`:

```ruby
arr.map { |h| h.values }
# => [[1, 2], [3, 4], [5, 6]]
```

Páginas relacionadas

Páginas semelhantes com exemplos

Em outros idiomas

Esta página está em outros idiomas

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................