Index by Property

Convert an array of objects to an object indexed by a property.

Code

General
arr.each_with_object({}) { |item, h| h[item[key]] = item }

Parameters

Array of objects

Property to use as index

Server

More Ruby Snippets