grouping objects using lodash
I have an array userList:
[
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
]
I wanted it to be like :
[
{
email : 'abc@gmail.com',
department : ['d1','d2']
},
{
email : 'dec@gmail.com',
department : ['d1']
}
]
I achieved it using :
const userGrp = _.groupBy(userList, 'email');
let data = ;
for (let key in userGrp) {
if (userGrp.hasOwnProperty(key)) {
const obj = {
email: key,
dept: extractDept(userGrp[key]),
};
data.push(obj);
}
}
function extractDept(userObjList) {
const arr = ;
userObjList.forEach((element) => {
arr.push(element.departmentName);
});
return arr;
}
How can I acheive it using lodash ?
javascript lodash
add a comment |
I have an array userList:
[
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
]
I wanted it to be like :
[
{
email : 'abc@gmail.com',
department : ['d1','d2']
},
{
email : 'dec@gmail.com',
department : ['d1']
}
]
I achieved it using :
const userGrp = _.groupBy(userList, 'email');
let data = ;
for (let key in userGrp) {
if (userGrp.hasOwnProperty(key)) {
const obj = {
email: key,
dept: extractDept(userGrp[key]),
};
data.push(obj);
}
}
function extractDept(userObjList) {
const arr = ;
userObjList.forEach((element) => {
arr.push(element.departmentName);
});
return arr;
}
How can I acheive it using lodash ?
javascript lodash
add a comment |
I have an array userList:
[
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
]
I wanted it to be like :
[
{
email : 'abc@gmail.com',
department : ['d1','d2']
},
{
email : 'dec@gmail.com',
department : ['d1']
}
]
I achieved it using :
const userGrp = _.groupBy(userList, 'email');
let data = ;
for (let key in userGrp) {
if (userGrp.hasOwnProperty(key)) {
const obj = {
email: key,
dept: extractDept(userGrp[key]),
};
data.push(obj);
}
}
function extractDept(userObjList) {
const arr = ;
userObjList.forEach((element) => {
arr.push(element.departmentName);
});
return arr;
}
How can I acheive it using lodash ?
javascript lodash
I have an array userList:
[
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
]
I wanted it to be like :
[
{
email : 'abc@gmail.com',
department : ['d1','d2']
},
{
email : 'dec@gmail.com',
department : ['d1']
}
]
I achieved it using :
const userGrp = _.groupBy(userList, 'email');
let data = ;
for (let key in userGrp) {
if (userGrp.hasOwnProperty(key)) {
const obj = {
email: key,
dept: extractDept(userGrp[key]),
};
data.push(obj);
}
}
function extractDept(userObjList) {
const arr = ;
userObjList.forEach((element) => {
arr.push(element.departmentName);
});
return arr;
}
How can I acheive it using lodash ?
javascript lodash
javascript lodash
asked Nov 19 '18 at 12:11
SamuelSamuel
1221219
1221219
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Since it's quite easy using plain js, here's the plain js way to group as you like
const grouped = [...foo.reduce((a, {email, department}) =>
a.set(email, (a.get(email) || ).concat(department))
, new Map)].map(([email, department]) => ({email, department}));
console.log(grouped)<script>
const foo = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
</script>add a comment |
Here is a working example with Lodash : http://jsfiddle.net/houssein/u4dzLk2b/1/
var data = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
var result = _.chain(data)
.groupBy("email")
.pairs()
.map(function (currentItem) {
return _.object(_.zip(["email", "departement"], currentItem));
})
.value();
console.log(result);
your outputdepartmentproperty is not a array of string
– Samuel
Nov 19 '18 at 12:30
add a comment |
With Lodash grouping on email and using entries and the "maping" to get the required output:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const result = _(data)
.groupBy('email')
.entries()
.map(([k,v]) => ({email: k, departments: _.map(v, 'department')}))
.value()
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>With ES6:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const grp = data.reduce((r,c) => (r[c.email] = [...r[c.email] || , c], r), {})
console.log(Object.entries(grp).map(([k,v]) =>
({email: k, departments: v.map(x => x.department)})))add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374385%2fgrouping-objects-using-lodash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since it's quite easy using plain js, here's the plain js way to group as you like
const grouped = [...foo.reduce((a, {email, department}) =>
a.set(email, (a.get(email) || ).concat(department))
, new Map)].map(([email, department]) => ({email, department}));
console.log(grouped)<script>
const foo = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
</script>add a comment |
Since it's quite easy using plain js, here's the plain js way to group as you like
const grouped = [...foo.reduce((a, {email, department}) =>
a.set(email, (a.get(email) || ).concat(department))
, new Map)].map(([email, department]) => ({email, department}));
console.log(grouped)<script>
const foo = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
</script>add a comment |
Since it's quite easy using plain js, here's the plain js way to group as you like
const grouped = [...foo.reduce((a, {email, department}) =>
a.set(email, (a.get(email) || ).concat(department))
, new Map)].map(([email, department]) => ({email, department}));
console.log(grouped)<script>
const foo = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
</script>Since it's quite easy using plain js, here's the plain js way to group as you like
const grouped = [...foo.reduce((a, {email, department}) =>
a.set(email, (a.get(email) || ).concat(department))
, new Map)].map(([email, department]) => ({email, department}));
console.log(grouped)<script>
const foo = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
</script>const grouped = [...foo.reduce((a, {email, department}) =>
a.set(email, (a.get(email) || ).concat(department))
, new Map)].map(([email, department]) => ({email, department}));
console.log(grouped)<script>
const foo = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
</script>const grouped = [...foo.reduce((a, {email, department}) =>
a.set(email, (a.get(email) || ).concat(department))
, new Map)].map(([email, department]) => ({email, department}));
console.log(grouped)<script>
const foo = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
</script>answered Nov 19 '18 at 12:16
bambambambam
45.1k873117
45.1k873117
add a comment |
add a comment |
Here is a working example with Lodash : http://jsfiddle.net/houssein/u4dzLk2b/1/
var data = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
var result = _.chain(data)
.groupBy("email")
.pairs()
.map(function (currentItem) {
return _.object(_.zip(["email", "departement"], currentItem));
})
.value();
console.log(result);
your outputdepartmentproperty is not a array of string
– Samuel
Nov 19 '18 at 12:30
add a comment |
Here is a working example with Lodash : http://jsfiddle.net/houssein/u4dzLk2b/1/
var data = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
var result = _.chain(data)
.groupBy("email")
.pairs()
.map(function (currentItem) {
return _.object(_.zip(["email", "departement"], currentItem));
})
.value();
console.log(result);
your outputdepartmentproperty is not a array of string
– Samuel
Nov 19 '18 at 12:30
add a comment |
Here is a working example with Lodash : http://jsfiddle.net/houssein/u4dzLk2b/1/
var data = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
var result = _.chain(data)
.groupBy("email")
.pairs()
.map(function (currentItem) {
return _.object(_.zip(["email", "departement"], currentItem));
})
.value();
console.log(result);
Here is a working example with Lodash : http://jsfiddle.net/houssein/u4dzLk2b/1/
var data = [
{email : 'abc@gmail.com',
department : 'd1'},
{email : 'abc@gmail.com',
department : 'd2'},
{email : 'dec@gmail.com',
department : 'd1'},
];
var result = _.chain(data)
.groupBy("email")
.pairs()
.map(function (currentItem) {
return _.object(_.zip(["email", "departement"], currentItem));
})
.value();
console.log(result);
edited Nov 19 '18 at 12:27
answered Nov 19 '18 at 12:21
Houssein ZouariHoussein Zouari
373210
373210
your outputdepartmentproperty is not a array of string
– Samuel
Nov 19 '18 at 12:30
add a comment |
your outputdepartmentproperty is not a array of string
– Samuel
Nov 19 '18 at 12:30
your output
department property is not a array of string– Samuel
Nov 19 '18 at 12:30
your output
department property is not a array of string– Samuel
Nov 19 '18 at 12:30
add a comment |
With Lodash grouping on email and using entries and the "maping" to get the required output:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const result = _(data)
.groupBy('email')
.entries()
.map(([k,v]) => ({email: k, departments: _.map(v, 'department')}))
.value()
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>With ES6:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const grp = data.reduce((r,c) => (r[c.email] = [...r[c.email] || , c], r), {})
console.log(Object.entries(grp).map(([k,v]) =>
({email: k, departments: v.map(x => x.department)})))add a comment |
With Lodash grouping on email and using entries and the "maping" to get the required output:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const result = _(data)
.groupBy('email')
.entries()
.map(([k,v]) => ({email: k, departments: _.map(v, 'department')}))
.value()
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>With ES6:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const grp = data.reduce((r,c) => (r[c.email] = [...r[c.email] || , c], r), {})
console.log(Object.entries(grp).map(([k,v]) =>
({email: k, departments: v.map(x => x.department)})))add a comment |
With Lodash grouping on email and using entries and the "maping" to get the required output:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const result = _(data)
.groupBy('email')
.entries()
.map(([k,v]) => ({email: k, departments: _.map(v, 'department')}))
.value()
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>With ES6:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const grp = data.reduce((r,c) => (r[c.email] = [...r[c.email] || , c], r), {})
console.log(Object.entries(grp).map(([k,v]) =>
({email: k, departments: v.map(x => x.department)})))With Lodash grouping on email and using entries and the "maping" to get the required output:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const result = _(data)
.groupBy('email')
.entries()
.map(([k,v]) => ({email: k, departments: _.map(v, 'department')}))
.value()
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>With ES6:
const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const grp = data.reduce((r,c) => (r[c.email] = [...r[c.email] || , c], r), {})
console.log(Object.entries(grp).map(([k,v]) =>
({email: k, departments: v.map(x => x.department)})))const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const result = _(data)
.groupBy('email')
.entries()
.map(([k,v]) => ({email: k, departments: _.map(v, 'department')}))
.value()
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const result = _(data)
.groupBy('email')
.entries()
.map(([k,v]) => ({email: k, departments: _.map(v, 'department')}))
.value()
console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const grp = data.reduce((r,c) => (r[c.email] = [...r[c.email] || , c], r), {})
console.log(Object.entries(grp).map(([k,v]) =>
({email: k, departments: v.map(x => x.department)})))const data = [{ email: 'abc@gmail.com', department: 'd1' }, { email: 'abc@gmail.com', department: 'd2' }, { email: 'dec@gmail.com', department: 'd1' }, ]
const grp = data.reduce((r,c) => (r[c.email] = [...r[c.email] || , c], r), {})
console.log(Object.entries(grp).map(([k,v]) =>
({email: k, departments: v.map(x => x.department)})))answered Nov 19 '18 at 17:10
AkrionAkrion
9,42711224
9,42711224
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374385%2fgrouping-objects-using-lodash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown