I am trying to do two groupBy on an array of objects ( essentially I am trying to chain two 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.
What should I be using instead of lodash.map()
or do I need to apply some processing before returning from map.
repl.it link to the code: 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. For example in the below function you can pass name
, age
, pet
so the object will be grouped by this key name.
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; it's just that somehow the object contain inside is shortened to [object]
If you access the object directly using console.log, it does work.
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]);
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.