[英]How to groupby in lodash for each item in a nested array
I have a json array of the form: 我有一个形式的json数组:
[{
"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: 我尝试使用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.
任何帮助表示赞赏。
You can use reduce and forEach 您可以使用reduce和forEach
Here idea is 这里的主意是
obj
variable. obj
变量的每个元素。 tags
property. tags
属性。 op
already has that tag we push the value else we add a new key to op
object with respective value op
已经具有该标签,然后我们推入该值,否则我们向具有相应值的op
对象添加一个新键 let 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)
You can use reduce
& forEach
as an alternative of loadash
. 您可以使用
reduce
& forEach
替代loadash
。 Inside reduce callback function iterate the tags
and check in the accumulator object if there exist a key
by this name. 在reduce回调函数中,对
tags
进行迭代,并检查累加器对象是否存在该名称的key
。 If it exist then push the current object , else create a key
and push value
如果存在,则推送当前对象,否则创建一个
key
并推送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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.