{"ScriptPreparationCode":"var data = [\r\n { name: \u0027pi\u0027, value: 3.14 },\r\n { name: \u0027hundred\u0027, value: 100 },\r\n { name: \u0027truthy\u0027, value: true },\r\n { name: \u0027falsy\u0027, value: false },\r\n { name: \u0027estring\u0027, value: \u00272.71828\u0027 },\r\n];\r\n\r\n\r\nvar keyBy = (array, fn) =\u003E {\r\n return array.reduce((acc, value) =\u003E {\r\n acc[fn(value)] = value;\r\n return acc;\r\n }, {});\r\n};\r\n\r\nvar keyByReduce = (array, key) =\u003E {\r\n return (array || []).reduce((r, x) =\u003E ({ ...r, [key ? x[key] : x]: x }), {})\r\n}\r\n\r\nvar keyByForEach = (array, fn) =\u003E {\r\n var acc = {};\r\n _.forEach(array, (value) =\u003E {\r\n acc[fn(value)] = value;\r\n });\r\n return acc;\r\n};\r\n\r\nvar keyByForLoop = (array, fn) =\u003E {\r\n var acc = {};\r\n for (let i = 0; i \u003C array.length; i\u002B\u002B) {\r\n acc[fn(array[i])] = array[i];\r\n }\r\n return acc;\r\n};","TestCases":[{"Name":"lodash/keyBy","Code":"_.keyBy(data, (v) =\u003E v.name);","IsDeferred":false},{"Name":"keyBy with native reduce","Code":"keyBy(data, (v) =\u003E v.name);","IsDeferred":false},{"Name":"keyBy with forEach","Code":"keyByForEach(data, (v) =\u003E v.name);","IsDeferred":false},{"Name":"keyBy with for loop","Code":"keyByForLoop(data, (v) =\u003E v.name);","IsDeferred":false},{"Name":"keyByReduce","Code":"keyByReduce(data, \u0027name\u0027);","IsDeferred":false}]}