简体   繁体   English

从lodash中的map()返回对象数组上groupBy()的结果将导致[object]

[英]Returning results of groupBy() on an array of objects from a map() in lodash results in [object]

I am trying to do two groupBy on an array of objects ( essentially I am trying to chain two groupBy) 我正在尝试对对象数组执行两个groupBy( 本质上,我正在尝试链接两个groupBy)

let lodash = require('lodash');
var characters = [
  { 'name': 'barney', 'age': 42,  'pet': 'dog' },
  { 'name': 'fred',   'age': 35,  'pet': 'dog' },
  { 'name': 'barney', 'age': 42,  'pet': 'cat' },
  { 'name': 'fred',   'age': 35,  'pet': 'goldfish' }
];

var groupByAge = lodash.groupBy(characters, 'age');

//console.log(groupByAge);

var groupByAgeAndPet = lodash.map(groupByAge, function(value){
  return lodash.groupBy(value, 'pet');

});

console.log(groupByAgeAndPet);

The output, however, is: 但是,输出为:

[ { dog: [ [Object] ], goldfish: [ [Object] ] }, { dog: [ [Object] ], cat: [ [Object] ] } ] [{狗:[[对象]],金鱼:[[对象]]},{狗:[[对象]],猫:[[对象]]}]

If I do a console.log() within the map, I will get the result I want. 如果在地图中执行console.log(),我将得到想要的结果。

What should I be using instead of lodash.map() or do I need to apply some processing before returning from map. 我应该使用什么代替lodash.map()还是需要做一些处理才能从地图返回。

repl.it link to the code: https://repl.it/@Kun_XinXin/OptimalUnkemptTrapezoids repl.it链接到代码: https ://repl.it/@Kun_XinXin/OptimalUnkemptTrapezoids

Here's the expected return: 这是预期的回报:

{
   35: { dog: [ { name: 'barney', age: 42, pet: 'dog' } ],
  cat: [ { name: 'barney', age: 42, pet: 'cat' } ] },
   42:{ dog: [ { name: 'fred', age: 35, pet: 'dog' } ],
  goldfish: [ { name: 'fred', age: 35, pet: 'goldfish' } ] }
}

With native javascript and array reduce function the same output can be achieved.Create a function and pass a parameter by which the objects will be grouped. 使用原生javascript和array reduce函数可以实现相同的输出。创建一个函数并传递一个参数,通过该参数可以对对象进行分组。 For example in the below function you can pass name , age , pet so the object will be grouped by this key name. 例如,在下面的函数中,您可以传递nameagepet以便按此键名称对对象进行分组。

 var characters = [{ 'name': 'barney', 'age': 42, 'pet': 'dog' }, { 'name': 'fred', 'age': 35, 'pet': 'dog' }, { 'name': 'barney', 'age': 42, 'pet': 'cat' }, { 'name': 'fred', 'age': 35, 'pet': 'goldfish' } ]; // creating a function which accepts an parameter which will be used to // group the objects function groupBy(keyName) { return characters.reduce(function(acc, curr, currIndex) { // checking if the object have any key by which the object will be grouped if (!acc.hasOwnProperty(curr[keyName])) { // if not create a key acc[curr[keyName]] = []; acc[curr[keyName]].push(curr) } // else just push the value acc[curr[keyName]].push(curr); return acc; }, {}) } // replace pet with name or age to group result by that key console.log(groupBy('pet')) 

In case anyone come across this while using console.log to debug such cases, it is actually working; 如果有人在使用console.log调试此类情况时遇到此问题,则它实际上可以正常工作; it's just that somehow the object contain inside is shortened to [object] 只是某种程度上,包含在其中的对象被缩短为[object]

If you access the object directly using console.log, it does work. 如果您使用console.log直接访问该对象,则它确实起作用。

let lodash = require('lodash');
var characters = [
  { 'name': 'barney', 'age': 42,  'pet': 'dog' },
  { 'name': 'fred',   'age': 35,  'pet': 'dog' },
  { 'name': 'barney', 'age': 42,  'pet': 'cat' },
  { 'name': 'fred',   'age': 35,  'pet': 'goldfish' }
];

var groupByAge = lodash.groupBy(characters, 'age');

//console.log(groupByAge);

var groupByAgeAndPet = lodash.mapValues(groupByAge, function(value){
  return lodash.groupBy(value, 'pet');

});

// this will display the actual object instead of the string 
console.log(groupByAgeAndPet[35]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM