I just started working with lodash and was wondering what better ways are there to achieve my goals. I am getting the data from a third-party API and cannot filter it beforehand. I have the following array:
[
[
User1 {
attr1: 'abc',
attr2: ['0x1', '0xa']
},
User2 {
attr1: 'def'
attr2: ['0x1', '0xb']
}
]
[
User3 {
attr1: 'ghi',
attr2: ['0x1', '0xa']
},
User4 {
attr1: 'jkl',
attr2: ['0x1', '0xb']
},
User5 {
attr1: 'mno',
attr2: ['0x1', '0xc']
}
]
]
The goal is to get the following. Essentially, I want to group users based on attr2[1] and filter out users that don't have a pair. The key would be the attr2[1].
[
{
attr2_1: '0xa',
attrs: [User1, User3]
},
{
attr2_1: '0xb',
attrs: [User2, User4]
}
]
I have a working solution but I would like to know how to solve it more elegantly.
I group the users by the second attribute
const usersByParam = _.chain(allUsers)
.flatten()
.groupBy(user => user.attr2[1])
.value()
I filter the grouped users to remove entries that don't have a pair (User5 in the example)
const filteredUsers = _.omitBy(usersByParam, (val) => {return val.length < 2})
I go through the filtered results to get my user objects
for (const key in filteredUsers) {
userArray.push({attr2_1: key, attrs: filteredUsers[key]} as User)
}
It gives me what I want but I am sure there are more elegant ways of solving it. Any pointers would be appreciated.
CodePudding user response:
The data that you posted is not valid JSON. So, assuming that it is supposed to be an array of objects, I modified it in my code below to reflect that.
I'm not sure how to do it in lodash, but you can just use vanilla JavaScript to accomplish this. By using the Array.reduce() and Array.filter() methods, you can manipulate the data to how you need.
See snippet below.
const data = [{
User1: {
attr1: 'abc',
attr2: ['0x1', '0xa']
}
},
{
User2: {
attr1: 'def',
attr2: ['0x1', '0xb']
}
},
{
User3: {
attr1: 'ghi',
attr2: ['0x1', '0xa']
}
},
{
User4: {
attr1: 'jkl',
attr2: ['0x1', '0xb']
}
},
{
User5: {
attr1: 'mno',
attr2: ['0x1', '0xc']
}
}
];
const reduced = Object.values(data.reduce((a, c) => {
var user = Object.keys(c)[0];
var key = c[user].attr2[1];
a[key] ??= {};
a[key].attr2_1 = key;
a[key].attrs ??= [];
a[key].attrs.push(user);
return a;
}, {})).filter(element => element.attrs.length === 2);
console.log(reduced);
CodePudding user response:
Your code is already elegant, I just replaced the for ... in
with map
const userArray = _.chain(allUsers)
.flatten()
.groupBy(user => user.attr2[1])
.omitBy(val => val.length < 2)
.map((attrs, attr2_1) => ({ attr2_1, attrs }))
.value();
CodePudding user response:
You can use JavaScript:
const data = [{User1: {attr1: 'abc',attr2: ['0x1', '0xa']}},{User2: {attr1: 'def',attr2: ['0x1', '0xb']}},{User3: {attr1: 'ghi',attr2: ['0x1', '0xa']}},{User4: {attr1: 'jkl',attr2: ['0x1', '0xb']}},{User5: {attr1: 'mno',attr2: ['0x1', '0xc']}}]
const result = Object.entries(
data.reduce((a, c) => {
const [k] = Object.keys(c)
a[c[k].attr2[1]] = [...(a[c[k].attr2[1]] || []), k]
return a
}, {})
)
.filter(([, users]) => users.length > 1)
.map(([attr2_1, attrs]) => ({ attr2_1, attrs }))
console.log(result)