[英]Use lodash groupBy function to categorize objects in an array
i have an array of products that each product has a category object. 我有一系列产品,每个产品都有一个类别对象。 I need to organize by category and include the category object.
我需要按类别组织并包括类别对象。 GroupBy function include only one parameter.
GroupBy函数仅包含一个参数。
the array of products 产品系列
const data = [
{id: 1, 'name': 'produto1', category: {id: 1, name: 'shirts', description: 'super roupa'}},
{id: 2, 'name': 'produto2', category: {id: 1, name: 'shirts', description: 'super roupa'}},
{id: 3, 'name': 'produto3', category: {id: 2, name: 'jackets', description: 'super jackets'}},
{id: 4, 'name': 'produto4', category: {id: 2, name: 'jackets', description: 'super jackets'}},
]
expected result: 预期结果:
[
{
category: {id: 1, name: 'clothes', description: 'super roupa'},
products:[{id:1, name: 'produt1'}, {id: 2, name: 'produto1'} ]
},
{
category: {id: 2, name: 'jackets', description: 'super jackets'},
products:[{id:3, name: 'produt3'}, {id: 4, name: 'produto4'} ]
},
]
Group by the category.id
, and then map the each group to an object by taking the category
from the 1st item in the group, and omitting category
from all products: 由组
category.id
,然后通过取映射每个组到对象category
从第一项目组中的,并省略category
从所有产品:
const data = [{"id":1,"name":"produto1","category":{"id":1,"name":"shirts","description":"super roupa"}},{"id":2,"name":"produto2","category":{"id":1,"name":"shirts","description":"super roupa"}},{"id":3,"name":"produto3","category":{"id":2,"name":"jackets","description":"super jackets"}},{"id":4,"name":"produto4","category":{"id":2,"name":"jackets","description":"super jackets"}}] const result = _(data) .groupBy('category.id') .map(group => ({ category: _.head(group).category, products: _.map(group, o => _.omit(o, 'category')) })) .value() console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
Or the _.flow()
function equivalent with lodash/fp: 或与lodash / fp等效的
_.flow()
函数:
const { flow, groupBy, map, head, omit } = _ const fn = flow( groupBy('category.id'), map(group => ({ category: head(group).category, products: map(omit('category'), group) })) ) const data = [{"id":1,"name":"produto1","category":{"id":1,"name":"shirts","description":"super roupa"}},{"id":2,"name":"produto2","category":{"id":1,"name":"shirts","description":"super roupa"}},{"id":3,"name":"produto3","category":{"id":2,"name":"jackets","description":"super jackets"}},{"id":4,"name":"produto4","category":{"id":2,"name":"jackets","description":"super jackets"}}] const result = fn(data) console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
Here's a solution without lodash: 这是一个没有lodash的解决方案:
You could reduce
the data array. 您可以
reduce
数据数组。 Destructure the parameter to get category
and rest
of the properties separately. 解构的参数来获取
category
和rest
分开的属性。 Here rest
will have id
and name
properties. rest
将具有id
和name
属性。 Then create an accumulator object with each unique category's id
as key. 然后使用每个唯一类别的
id
作为键创建一个累加器对象。 Set the value to be the final objects needed in the output. 将值设置为输出中所需的最终对象。 If the key already exists, update it's
products
array. 如果密钥已经存在,请更新它的
products
数组。 Else, add a new key to the accumulator. 否则,向累加器添加一个新密钥。 Then finally use
Object.values()
to convert this accumulator object to an array of required values 然后,最后使用
Object.values()
将该累加器对象转换为所需值的数组
const data = [{"id":1,"name":"produto1","category":{"id":1,"name":"shirts","description":"super roupa"}},{"id":2,"name":"produto2","category":{"id":1,"name":"shirts","description":"super roupa"}},{"id":3,"name":"produto3","category":{"id":2,"name":"jackets","description":"super jackets"}},{"id":4,"name":"produto4","category":{"id":2,"name":"jackets","description":"super jackets"}}] const merged = data.reduce((acc, { category, ...rest }) => { acc[category.id] = acc[category.id] || { category, products: [] }; acc[category.id].products.push(rest); return acc; }, {}) console.log(Object.values(merged))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.