gpt4 book ai didi

javascript - lodash 动态分组

转载 作者:太空宇宙 更新时间:2023-11-04 16:02:30 28 4
gpt4 key购买 nike

目前正在使用 lodash 制作具有平面数据结构的树。但手动传递属性来生成组。我想将树创建函数与两个参数分开

function makeTree(data,properties){}

当前数据结构

var data =  [
{"taskID": 1,"taskTitle": "Project Meeting","tagID": 23,"tagName": "IT Project V4","tagTypeID": 2,"tagType": "Project"},
{"taskID": 1,"taskTitle": "Project Meeting","tagID": 5,"tagName": "Customer 1","tagTypeID": 1,"tagType": "Customer"},
{"taskID": 1,"taskTitle": "Project Meeting","tagID": 19,"tagName": "Project 3","tagTypeID": 2,"tagType": "Project"},
{"taskID": 1,"taskTitle": "Project Meeting","tagID": 14,"tagName": "Internal Dev","tagTypeID": 3,"tagType": "Development"},
{"taskID": 1,"taskTitle": "Project Meeting","tagID": 4,"tagName": "Customer 2","tagTypeID": 1,"tagType": "Customer"},
{"taskID": 2,"taskTitle": "Weekly Team Meeting","tagID": 10,"tagName": "IT Project","tagTypeID": 2,"tagType": "Project"},
{"taskID": 2,"taskTitle": "Weekly Team Meeting","tagID": 11,"tagName": "Project 2","tagTypeID": 2,"tagType": "Project"},
{"taskID": 2,"taskTitle": "Weekly Team Meeting","tagID": 14,"tagName": "Internal Dev","tagTypeID": 3,"tagType": "Development"},
{"taskID": 2,"taskTitle": "Weekly Team Meeting","tagID": 23,"tagName": "IT Project V4","tagTypeID": 2,"tagType": "Project"},
{"taskID": 3,"taskTitle": "Release Planing","tagID": 10,"tagName": "IT Project","tagTypeID": 2,"tagType": "Project"},
{"taskID": 3,"taskTitle": "Release Planing","tagID": 7,"tagName": "Inpex","tagTypeID": 1,"tagType": "Customer"},
{"taskID": 3,"taskTitle": "Release Planing","tagID": 2,"tagName": "Agrium","tagTypeID": 1,"tagType": "Customer"},
{"taskID": 3,"taskTitle": "Release Planing","tagID": 3,"tagName": "Sain Gobain","tagTypeID": 1,"tagType": "Customer"},
{"taskID": 3,"taskTitle": "Release Planing","tagID": 4,"tagName": "Customer 2","tagTypeID": 1,"tagType": "Customer"},
{"taskID": 3,"taskTitle": "Release Planing","tagID": 5,"tagName": "Customer 1","tagTypeID": 1,"tagType": "Customer"}

];



var output= _(data).groupBy('tagType').map(function (value, key) {
return {
value: key,
children : _(value).groupBy('tagName').map(function(val,key){
return { value : key, children : _(val).groupBy('taskTitle').map(function(v,k){ return { value : v,children:[]}}).value() }
}).value()
}
}).value();

console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

最佳答案

尝试这个递归解决方案。好处是它可以与任意数量的属性一起进行分组。

let makeTree = function (data, properties) {
let curPropName = properties[0];
let remainingProps = _.drop(properties, 1);

return _(data)
.groupBy(curPropName)
.map(function (val, key) {
if (properties.length > 1) {
return {
value: key,
children: makeTree(val, remainingProps)
};
} else {
return {value: val, children: []};
}
})
.value();
};

//To get the same result as you get now call it like this
let tree = makeTree(data, ['tagType', 'tagName', 'taskTitle']);

关于javascript - lodash 动态分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42161644/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com