Javascript 如何在lodash中为嵌套数组中的每个项创建groupby
我有一个json数组,格式如下:Javascript 如何在lodash中为嵌套数组中的每个项创建groupby,javascript,arrays,json,grouping,lodash,Javascript,Arrays,Json,Grouping,Lodash,我有一个json数组,格式如下: [{ "published": true, "tags": ["tag1", "tag2"], "categories": ["cat1"], "author": "some name", "post-format": "standard", "title": "Second Post,", "url-slug": "second-post",
[{
"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"
}]
我想将它们按标签分组,格式如下:
[{
"tag1": [{...}, {...}],
"tag2": [{...}, {...}],
"tag3": [{...}]
}]
我曾尝试使用lodash来实现这一点:
const groupedByTag = _.groupBy(blogMetadata, function(postmetadata) {
postmetadata.tags.map(tag => {
return tag
})
})
显然,上面的代码是不正确的,不起作用。我看了一份相关的报告,但没有取得多大进展。非常感谢您的帮助。您可以使用和
这是我的想法
- 首先,我们循环遍历
变量的每个元素obj
- 对于每个元素,我们循环使用
属性标记
- 我们检查
是否已经有了该标记,我们推送该值,否则我们将向op
对象添加一个具有相应值的新键op
let obj=[{“published”:true,“tags”:[“tag1”,“tag2”],“categories”:[“cat1”],“author”:“some name”,“post format”:“standard”,“title”:“Second post”,“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.”摘录”:“Lorem ipsum dolor sit amet,Concertetur Adipising Elite,sed do eiusmod Temporal incididunt”,“path”:“2019/03/28/SecondPost.md”},{“published”:true,“tags”:[“tag1”,“tag2”,“tag3”],“categories”:[“cat1”,“cat2”],“author”:“一些名字”,“post格式”:“标准”,“title”:“入门”,“url slug”:“入门”,“首次发布日期”:”2019-03-20,“最新更新日期”:“2019-03-20”,“meta”:{“标题”:“入门”,“描述”:“入门帖子”},“摘录”:“Lorem ipsum dolor sit amet,Concertetur Adipising Elite,sed do eiusmod Temporal Incidedunt”,“路径”:“2019/03/20/GettingStarted.md”]
让最终=目标减少((op,inp)=>{
inp.tags.forEach(e=>{
op[e]=op[e]| |[]
op[e].push(JSON.parse(JSON.stringify(inp)))
})
返回操作
},{})
console.log(最终版)
您可以使用reduce
和forEach
作为loadash
的替代方法。在reduce回调函数中,迭代标记
,检查累加器对象中是否存在同名的键。如果存在,则按下当前对象,否则创建键
并按下值代码>
let数据=[{
“出版”:没错,
“标记”:[“标记1”、“标记2”],
“类别”:[“cat1”],
“作者”:“某个名字”,
“发布格式”:“标准”,
“职务”:“第二职位”,
“url slug”:“第二篇文章”,
“首次发布日期”:“2019-03-28”,
“最后更新日期”:“2019-03-28”,
“元”:{
“标题”:“第二篇文章”,
“说明”:“第二篇文章。”
},
“节选”为“知识产权保护、教育和发展精英、教育和社会发展临时委员会”,
“路径”:“2019/03/28/SecondPost.md”
}, {
“出版”:没错,
“标记”:[“标记1”、“标记2”、“标记3”],
“类别”:[“第1类”、“第2类”],
“作者”:“某个名字”,
“发布格式”:“标准”,
“标题”:“入门”,
“url slug”:“入门”,
“首次发布日期”:“2019-03-20”,
“最后更新日期”:“2019-03-20”,
“元”:{
“标题”:“入门”,
“说明”:“入门贴子”
},
“节选”为“知识产权保护、教育和发展精英、教育和社会发展临时委员会”,
“路径”:“2019/03/20/GettingStarted.md”
}];
让newMapped=[data.reduce(函数(acc,curr)){
当前标记forEach(功能(项目){
如果(附件[项目]){
acc[项目].推送(当前)
}否则{
acc[项目]=[当前]
}
})
返回acc;
}, {})];
log(newMapped)
您可以尝试reduce或“json groupby”节点JS模块。