Lodash的Dyamic分组

时间:2017-02-10 14:20:50

标签: javascript tree group-by lodash

目前使用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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

尝试这种递归解决方案。 好处是,它可以与任意数量的属性一起使用。

&#13;
&#13;
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']);
&#13;
&#13;
&#13;