Výpis hodnoty z poľa ruby

Príklady kódu

0
0

ruby extrahovať prvky z poľa

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]]
```

Podobné stránky

Podobné stránky s príkladmi

V iných jazykoch

Táto stránka je v iných jazykoch

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