lodash indexBy with not uniq keys
lodash indexBy with not uniq keys
我如何使用不使用 uniq 键索引数组。我尝试使用 lodash indexBy,但它没有给出预期的结果。
var keys = [
{ 'dir': 'left', 'code': 97 },
{ 'dir': 'left', 'code': 100 },
{ 'dir': 'right', 'code': 50 },
{ 'dir': 'right', 'code': 51 }
];
var a = _.indexBy(keys, 'dir');
结果:
{ left: { dir: 'left', code: 100 },
right: { dir: 'right', code: 51 } }
预期成果:
{ left: [{ dir: 'left', code: 100 }, { 'dir': 'left', 'code': 97 }],
right: [{ dir: 'right', code: 51 }, { 'dir': 'right', 'code': 50 }] }
你需要为此使用_.groupBy
,就像这样
console.log(_.groupBy(keys, 'dir'));
会打印
{ left: [ { dir: 'left', code: 97 }, { dir: 'left', code: 100 } ],
right: [ { dir: 'right', code: 50 }, { dir: 'right', code: 51 } ] }
相关文章:
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- JSON.stringify和Object.keys在同一个对象上产生不同的结果
- 使用lodash从对象数组中获取uniq[key,value]
- AngularJS错误:请求的键的值不是对象.keys@[本地代码]
- 相当于Ruby中的pull和uniq的Javascript
- Angularjs orderby objects keys
- 为什么 Object.keys() 和 for ..在不同的
- React JS无法读取属性'keys'的未定义
- 用javascript克隆整个html表(带有uniq ID)元素
- 如何避免在object.keys中不存在obj时发生错误
- 对非对象调用了gulp jscs Object.keys
- 为什么Object.keys显示id为key
- 正在javascript中检索keys值
- 神秘的行,用于在 Webpack 中需要 html 模板:templates.keys().forEach(templa
- Safari上的Selenium网络驱动程序:Keys.ENTER不起作用
- array.sort() 是否就地,即使在 Object.keys() 上操作也是如此
- 了解 JavaScript 中的 Object.keys
- Object.keys(obj).length 返回字符总和而不是记录总数
- Table of objects - uniq
- lodash indexBy with not uniq keys