Javascript 如何在react js中基于类别字段对数据进行分组?
我想根据类别对数据进行分组 场 输入为:Javascript 如何在react js中基于类别字段对数据进行分组?,javascript,Javascript,我想根据类别对数据进行分组 场 输入为: var data: [ {id:1, title:”title 1”, Category: “services”}, {id:2 title:”title 2”, Category: “Blogs”}, {id:3, title:”title 3”, Category: “services”}, {id:4, title:”title 4”, Category: “services”}, {id:5, title:”Services”
var data: [
{id:1, title:”title 1”, Category: “services”},
{id:2 title:”title 2”, Category: “Blogs”},
{id:3, title:”title 3”, Category: “services”},
{id:4, title:”title 4”, Category: “services”},
{id:5, title:”Services”, Category: “Blogs”}
]
预期产出为:
var out_data: {
“services”:[
{id: 1, title: ”title 1”},
{id:3, title:”title 3”},
{id:4, title:”title 4”}
],
“Blogs”:[
{id:2 title:”title 2”},
{id:5, title:”Services”}
]
}
如果您知道,请提供任何解决方案。提前感谢。这里是数千种解决方案中的一种:)
以下是数千种解决方案中的一种:) 您可以使用Loadash的功能:
npm i——保存lodash.groupby
例如:
var groupBy = require('lodash.groupby');
var data = [
{ id: 1, title: 'title 1', Category: 'services' },
{ id: 2, title: 'title 2', Category: 'Blogs' },
{ id: 3, title: 'title 3', Category: 'services' },
{ id: 4, title: 'title 4', Category: 'services' },
{ id: 5, title: 'Services', Category: 'Blogs' }
]
var out_data = groupBy(data, 'Category');
console.log(out_data);
将输出:
{ services:
[ { id: 1, title: 'title 1', Category: 'services' },
{ id: 3, title: 'title 3', Category: 'services' },
{ id: 4, title: 'title 4', Category: 'services' } ],
Blogs:
[ { id: 2, title: 'title 2', Category: 'Blogs' },
{ id: 5, title: 'Services', Category: 'Blogs' } ] }
您可以使用Loadash的功能:
npm i——保存lodash.groupby
例如:
var groupBy = require('lodash.groupby');
var data = [
{ id: 1, title: 'title 1', Category: 'services' },
{ id: 2, title: 'title 2', Category: 'Blogs' },
{ id: 3, title: 'title 3', Category: 'services' },
{ id: 4, title: 'title 4', Category: 'services' },
{ id: 5, title: 'Services', Category: 'Blogs' }
]
var out_data = groupBy(data, 'Category');
console.log(out_data);
将输出:
{ services:
[ { id: 1, title: 'title 1', Category: 'services' },
{ id: 3, title: 'title 3', Category: 'services' },
{ id: 4, title: 'title 4', Category: 'services' } ],
Blogs:
[ { id: 2, title: 'title 2', Category: 'Blogs' },
{ id: 5, title: 'Services', Category: 'Blogs' } ] }
到现在为止你都试了些什么?“请提供任何解决方案”不是正确的说法。所以,正确的方法是向我们展示你尝试了什么以及你在哪里遇到了困难。到目前为止你尝试了什么?“请提供任何解决方案”不是正确的说法。所以,正确的方法是向我们展示你尝试了什么,以及你在哪里被卡住了。