对象的组数组,嵌套具有特定名称的某些键

对象的组数组,嵌套具有特定名称的某些键,第1张

对象的组数组嵌套具有特定名称的某些键

这可以用一个巧妙的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>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5110722.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存