Reputation: 541
I have a json array of the form:
[{
"published": true,
"tags": ["tag1", "tag2"],
"categories": ["cat1"],
"author": "some name",
"post-format": "standard",
"title": "Second Post,",
"url-slug": "second-post",
"first-published-on": "2019-03-28",
"last-updated-on": "2019-03-28",
"meta": {
"title": "Second Post",
"description": "Second post."
},
"excerpt": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt",
"path": "2019/03/28/SecondPost.md"
}, {
"published": true,
"tags": ["tag1", "tag2", "tag3"],
"categories": ["cat1", "cat2"],
"author": "some name",
"post-format": "standard",
"title": "Getting Started",
"url-slug": "getting-started",
"first-published-on": "2019-03-20",
"last-updated-on": "2019-03-20",
"meta": {
"title": "Getting Started",
"description": "Getting started post."
},
"excerpt": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt",
"path": "2019/03/20/GettingStarted.md"
}]
I would like to group them by tags, in a form below:
[{
"tag1": [{...}, {...}],
"tag2": [{...}, {...}],
"tag3": [{...}]
}]
I have tried to do so using lodash:
const groupedByTag = _.groupBy(blogMetadata, function(postmetadata) {
postmetadata.tags.map(tag => {
return tag
})
})
Obviously the code above is incorrect and does not work. I have looked at a related post, without making much headway. Any help is appreciated.
Upvotes: 1
Views: 710
Reputation: 37775
You can use reduce and forEach
Here idea is
obj
variable.tags
property.op
already has that tag we push the value else we add a new key to op
object with respective valuelet obj = [{"published": true,"tags": ["tag1", "tag2"],"categories": ["cat1"],"author": "some name","post-format": "standard","title": "Second Post,","url-slug": "second-post","first-published-on": "2019-03-28","last-updated-on": "2019-03-28","meta": {"title": "Second Post","description": "Second post."},"excerpt": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt","path": "2019/03/28/SecondPost.md"}, {"published": true,"tags": ["tag1", "tag2", "tag3"],"categories": ["cat1", "cat2"],"author": "some name","post-format": "standard","title": "Getting Started","url-slug": "getting-started","first-published-on": "2019-03-20","last-updated-on": "2019-03-20","meta": {"title": "Getting Started","description": "Getting started post."},"excerpt": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt","path": "2019/03/20/GettingStarted.md"}]
let final = obj.reduce((op,inp) => {
inp.tags.forEach(e => {
op[e] = op[e] || []
op[e].push(JSON.parse(JSON.stringify(inp)))
})
return op
},{})
console.log(final)
Upvotes: 1
Reputation: 50346
You can use reduce
& forEach
as an alternative of loadash
. Inside reduce callback function iterate the tags
and check in the accumulator object if there exist a key
by this name. If it exist then push the current object , else create a key
and push value
let data = [{
"published": true,
"tags": ["tag1", "tag2"],
"categories": ["cat1"],
"author": "some name",
"post-format": "standard",
"title": "Second Post,",
"url-slug": "second-post",
"first-published-on": "2019-03-28",
"last-updated-on": "2019-03-28",
"meta": {
"title": "Second Post",
"description": "Second post."
},
"excerpt": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt",
"path": "2019/03/28/SecondPost.md"
}, {
"published": true,
"tags": ["tag1", "tag2", "tag3"],
"categories": ["cat1", "cat2"],
"author": "some name",
"post-format": "standard",
"title": "Getting Started",
"url-slug": "getting-started",
"first-published-on": "2019-03-20",
"last-updated-on": "2019-03-20",
"meta": {
"title": "Getting Started",
"description": "Getting started post."
},
"excerpt": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt",
"path": "2019/03/20/GettingStarted.md"
}];
let newMapped = [data.reduce(function(acc, curr) {
curr.tags.forEach(function(item) {
if (acc[item]) {
acc[item].push(curr)
} else {
acc[item] = [curr]
}
})
return acc;
}, {})];
console.log(newMapped)
Upvotes: 1