Javascript 使用lodash按数组中的每个项分组
我从API中获得了这些类型的数据结构,他们让我对它们进行相应的分组 输入Javascript 使用lodash按数组中的每个项分组,javascript,data-structures,mapping,underscore.js,lodash,Javascript,Data Structures,Mapping,Underscore.js,Lodash,我从API中获得了这些类型的数据结构,他们让我对它们进行相应的分组 输入 { 0: { id: 0, name: 'foo', categories: [ 'Category001', 'Category002/sub-category001' ] }, 1: { id: 1, name: 'bar', categ
{
0: {
id: 0,
name: 'foo',
categories: [
'Category001',
'Category002/sub-category001'
]
},
1: {
id: 1,
name: 'bar',
categories: [
'Category002/sub-category001'
]
},
2: {
id: 2,
name: 'bazz',
categories: [
'Category001',
'Category002',
'Category003'
]
},
3: {
id: 3,
name: 'rem',
categories: [
'Category001/sub-category002/nth-category008',
'Category001/sub-category004',
'Category003/sub-category001'
]
}
}
{
0: {
"name": "Category001",
"isCategory": true,
"children": [
{
"id": 0,
"name": "foo",
"categoryPath": "Category001",
"isCategory": false
},
{
"id": 2,
"name": "bazz",
"categoryPath": "Category001",
"isCategory": false
},
{
"name": "sub-category004",
"categoryPath": "Category001/sub-category004",
"children": [
{
"id": 3,
"name": "rem",
"isCategory": false,
}
],
"isCategory": true
},
{
"name": "sub-category002",
"categoryPath": "Category001/sub-category002",
"children": [
{
"name": "sub-category008",
"categoryPath": "Category001/sub-category002/nth-category008",
"children": [
{
"id": 3,
"name": "rem",
"isCategory": false
}
],
"isCategory": true
},
],
"isCategory": true
},
{
"name": "sub-category002",
"categoryPath": "Category001/sub-category002",
"isCategory": true
}
],
"categoryPath": ""
},
1: {
"name": "Category002",
"isCategory": true,
"children": [
{
"id": 2,
"name": "bazz",
"categoryPath": "Category002",
"isCategory": false
},
{
"name": "sub-category001",
"categoryPath": "Category002/sub-category001",
"children": [
{
"id": 0,
"name": "foo",
"isCategory": false,
},
{
"id": 1,
"name": "bar",
"isCategory": false,
}
],
"isCategory": true
}
],
"categoryPath": ""
},
2: {
"name": "Category003",
"isCategory": true,
"children": [
{
"id": 2,
"name": "bazz",
"categoryPath": "Category002",
"isCategory": false
},
{
"name": "sub-category001",
"categoryPath": "Category003/sub-category001",
"children": [
{
"id": 0,
"name": "foo",
"isCategory": false,
}
],
"isCategory": true
}
],
"categoryPath": ""
}
}
所需输出
{
0: {
id: 0,
name: 'foo',
categories: [
'Category001',
'Category002/sub-category001'
]
},
1: {
id: 1,
name: 'bar',
categories: [
'Category002/sub-category001'
]
},
2: {
id: 2,
name: 'bazz',
categories: [
'Category001',
'Category002',
'Category003'
]
},
3: {
id: 3,
name: 'rem',
categories: [
'Category001/sub-category002/nth-category008',
'Category001/sub-category004',
'Category003/sub-category001'
]
}
}
{
0: {
"name": "Category001",
"isCategory": true,
"children": [
{
"id": 0,
"name": "foo",
"categoryPath": "Category001",
"isCategory": false
},
{
"id": 2,
"name": "bazz",
"categoryPath": "Category001",
"isCategory": false
},
{
"name": "sub-category004",
"categoryPath": "Category001/sub-category004",
"children": [
{
"id": 3,
"name": "rem",
"isCategory": false,
}
],
"isCategory": true
},
{
"name": "sub-category002",
"categoryPath": "Category001/sub-category002",
"children": [
{
"name": "sub-category008",
"categoryPath": "Category001/sub-category002/nth-category008",
"children": [
{
"id": 3,
"name": "rem",
"isCategory": false
}
],
"isCategory": true
},
],
"isCategory": true
},
{
"name": "sub-category002",
"categoryPath": "Category001/sub-category002",
"isCategory": true
}
],
"categoryPath": ""
},
1: {
"name": "Category002",
"isCategory": true,
"children": [
{
"id": 2,
"name": "bazz",
"categoryPath": "Category002",
"isCategory": false
},
{
"name": "sub-category001",
"categoryPath": "Category002/sub-category001",
"children": [
{
"id": 0,
"name": "foo",
"isCategory": false,
},
{
"id": 1,
"name": "bar",
"isCategory": false,
}
],
"isCategory": true
}
],
"categoryPath": ""
},
2: {
"name": "Category003",
"isCategory": true,
"children": [
{
"id": 2,
"name": "bazz",
"categoryPath": "Category002",
"isCategory": false
},
{
"name": "sub-category001",
"categoryPath": "Category003/sub-category001",
"children": [
{
"id": 0,
"name": "foo",
"isCategory": false,
}
],
"isCategory": true
}
],
"categoryPath": ""
}
}
问题
var groups = _.chain(items)
.groupBy('categories')
.pairs()
.value();
除了lodash函数之外,这里无法避免自定义处理。以下是最多使用lodash的尝试:
var transformed = _(input)
.transform(function (result, item) {
_(item.categories)
.map(function (categoryPath) {
return categoryPath.split('/');
})
.each(function (categoryPathParts) {
var dict = result;
var par;
var fullpath = _.reduce(categoryPathParts, function (path, category, i) {
path += (i > 0 ? '/' : '') + category;
if (!(par = _.find(dict, 'name', category))) {
dict.push(par = {
name: category,
categoryPath: (i > 0 ? path : ''),
isCategory: true,
children: []
});
}
dict = _.find(dict, 'name', category).children;
return path;
}, "")
par.children.push({
id: item.id,
name: item.name,
isCategory: false,
categoryPath: fullpath,
});
}).value();
}, [])
.transform(function (resObj, resCat, i) {
resObj[i] = resCat;
}, {});
var输入={
0: {
id:0,
名称:“foo”,
类别:[
“类别001”,
‘类别002/子类别001’]
},
1: {
id:1,
名称:'酒吧',
类别:[
‘类别002/子类别001’]
},
2: {
id:2,
名称:“bazz”,
类别:[
“类别001”,
“类别002”,
“类别003”]
},
3: {
id:3,
名称:“rem”,
类别:[
‘类别001/子类别002/nth-类别008’,
“类别001/子类别004”,
'类别003/子类别001']
}
};
已转换变量=(输入)
.转换(功能(结果、项目){
_(项目.类别)
.map(函数(categoryPath){
返回categoryPath.split('/');
})
.每个(功能(类别路径部件){
var dict=结果;
VAR;
var fullpath=\减少(categoryPathParts,函数(路径,类别,i){
路径+=(i>0?'/':'')+类别;
如果(!(段落=u.find(dict,'name',category))){
指令推送(par={
名称:类别,
categoryPath:(i>0?路径:“”),
分类:正确,
儿童:[]
});
}
dict=\查找(dict'name',category).children;
返回路径;
}, "")
儿童推杆({)
id:item.id,
名称:item.name,
分类:假,
类别路径:完整路径,
});
}).value();
}, [])
.transform(函数(resObj、resCat、i){
resObj[i]=重新扫描;
}, {});
document.getElementById('resultArea').textContent=JSON.stringify(已转换,null,2)代码>
textarea{
宽度:100%;
}
人们应该在投票前开始评论。在lodash中没有任何直接的方法可以做到这一点,你肯定需要编写自定义逻辑,尝试使用u.map转换,然后使用GroupBy。你能至少给我一个方向吗?Haseeb Asif似乎在谈论使用然后再使用。如果这些没有帮助,请告诉我们。