Data like this:
[
{type: "one", ...fields},
{type: "one", ...fields},
{type: "two", ...fields}
]
should be converted to this:
{
one: [...itemsWithType == "one"],
two: [...itemsWithType == "two"]
}
I know how to do it in vanillas ES5 / ES6.. I am looking for a special lodash command. I am just lost in the docs, and maybe someone can tell me how this preparation is called, that I can notice it.
I am looking for a simple lodash function like
mapByProp(collection, 'type');
Yes, this could be written like this one:
const mapByProp = (array, t) => {
return array.reduce((m, i) => {
!m[i[t]] && m[i[t]] = [];
m[i[t]].push(i);
return m;
}, {});
};
But if there is a lodash function, I would love to use it
You can use _.groupBy :
var a = [
{type: "one", wow: 1},
{type: "one", wow: 2},
{type: "two", wow: 3}
];
var result = _.groupBy(a, 'type');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.