简体   繁体   中英

How to groupby in lodash for each item in a nested array

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.

You can use reduce and forEach

Here idea is

  • First we loop through each element of obj variable.
  • For each element we loop through tags property.
  • We check if op already has that tag we push the value else we add a new key to op object with respective value

 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 . 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM