如何对嵌套数组中的每个项目使用lodash分组

时间:2019-04-01 07:33:56

标签: 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",
        "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
        })
      })

很显然,以上代码不正确,无法正常工作。我已经看过相关的post,但进展并不顺利。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

您可以使用reduceforEach

这里的想法是

  • 首先,我们遍历obj变量的每个元素。
  • 对于每个元素,我们遍历tags属性。
  • 我们检查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)

答案 1 :(得分:1)

您可以使用reduceforEach替代loadash。在reduce回调函数内部,迭代tags并检查累加器对象是否存在以此名称命名的key。如果存在,则推送当前对象,否则创建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)