这可以用一个巧妙的combinartion完成
_.map和
_.groupBy。
const items = [ { tab: 'Results', section: '2017', title: 'Full year Results', description: 'Something here', }, { tab: 'Results', section: '2017', title: 'Half year Results', description: 'Something here', }, { tab: 'Reports', section: 'Marketing', title: 'First Report', description: 'Something here', }];function groupAndMap(items, itemKey, childKey, predic){ return _.map(_.groupBy(items,itemKey), (obj,key) => ({ [itemKey]: key, [childKey]: (predic && predic(obj)) || obj }));}var result = groupAndMap(items,"tab","sections", arr => groupAndMap(arr,"section", "items"));console.log(result);<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)