https://cdn.jsdelivr.net/npm/
[email protected]
/lodash.min.js
{"ScriptPreparationCode":"\r\nfunction generateDataArray(template, count) {\r\n const dataArray = [];\r\n for (let i = 0; i \u003C count; i\u002B\u002B) {\r\n const newData = {\r\n ...template,\r\n name: template.name \u002B \u0022_\u0022 \u002B i,\r\n };\r\n dataArray.push(newData);\r\n }\r\n return dataArray;\r\n}\r\n\r\nconst templateData = {\r\n \u0022name\u0022: \u0022jim\u0022,\r\n \u0022color\u0022: \u0022blue\u0022,\r\n \u0022age\u0022: \u002222\u0022\r\n};\r\n\r\nvar data = generateDataArray(templateData, 1000);\r\n\r\n\r\nvar chainableFunctions = {\r\n map: _.map,\r\n groupBy: _.groupBy,\r\n value: _.value,\r\n};\r\n\r\nvar chain = (input) =\u003E {\r\n var value = input;\r\n var wrapper = {\r\n ..._.mapValues(\r\n chainableFunctions,\r\n (f) =\u003E (...args) =\u003E {\r\n // lodash always puts input as the first argument\r\n value = f(value, ...args);\r\n return wrapper;\r\n },\r\n ),\r\n value: () =\u003E value,\r\n };\r\n return wrapper;\r\n};\r\n","TestCases":[{"Name":"chain","Code":"console.log(\r\n chain(data)\r\n // Group the elements of Array based on \u0060color\u0060 property\r\n .groupBy(\u0022color\u0022)\r\n // \u0060key\u0060 is group\u0027s name (color), \u0060value\u0060 is the array of objects\r\n .map((value, key) =\u003E ({ color: key, users: value }))\r\n .value()\r\n);","IsDeferred":false},{"Name":"custom","Code":"var custom = (x) =\u003E {\r\n var group = _.groupBy(x, \u0027color\u0027);\r\n return _.map(_.entries(group), ([ color, val ]) =\u003E ({ color, val }))\r\n}\r\nconsole.log(custom(data))","IsDeferred":false}]}