Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按属性分类的地图集合项目lodash_Javascript_Lodash - Fatal编程技术网

Javascript 按属性分类的地图集合项目lodash

Javascript 按属性分类的地图集合项目lodash,javascript,lodash,Javascript,Lodash,数据如下: [ {type: "one", ...fields}, {type: "one", ...fields}, {type: "two", ...fields} ] 应转换为: { one: [...itemsWithType == "one"], two: [...itemsWithType == "two"] } 我知道如何在vanillas ES5/ES6中实现。。我正在寻找一个特殊的洛达斯命令。我只是在文件中迷失了方向,也许有人能告诉我这项准备工

数据如下:

[
   {type: "one", ...fields},
   {type: "one", ...fields},
   {type: "two", ...fields}
]
应转换为:

{
   one: [...itemsWithType == "one"],
   two: [...itemsWithType == "two"]
}
我知道如何在vanillas ES5/ES6中实现。。我正在寻找一个特殊的洛达斯命令。我只是在文件中迷失了方向,也许有人能告诉我这项准备工作叫什么,我会注意到的

我正在寻找一个简单的lodash函数,如

mapByProp(collection, 'type');
是的,可以这样写:

const mapByProp = (array, t) => {
   return array.reduce((m, i) => {
     !m[i[t]] && m[i[t]] = [];
     m[i[t]].push(i);

     return m;
   }, {});
};
但是如果有lodash函数,我很乐意使用它

您可以使用:

您可以使用:

var a = [
   {type: "one", wow: 1},
   {type: "one", wow: 2},
   {type: "two", wow: 3}
];

var result = _.groupBy(a, 'type');