Intermediate Enumerables • Exercise 5/5

Each With Object

00:00
0
20 points

Instructions

Create a function index_by_id that takes an array of hashes
(each with an :id key) and returns a hash indexed by id.

Use each_with_object to build a hash:
```ruby
[1, 2].each_with_object({}) { |n, hash| hash[n] = n * 2 }

=> {1 => 2, 2 => 4}


Example:
```ruby
users = [
  { id: 1, name: "Alice" },
  { id: 2, name: "Bob" }
]
index_by_id(users)
# => { 1 => { id: 1, name: "Alice" }, 2 => { id: 2, name: "Bob" } }

Your Code

Results

Click "Run Tests" to see results