我有 3 个对象
[ {name: 3, q: 10, b: 1}, {name: 5, q: 6, b: 2}, {name: 5, q: 7, b: 1} ]
我需要按名称对它们进行分组:
[ {name: 3: items: [{q:10, b: 1}]}, {name: 5: items: [{q:6, b: 2}, {q:7, b: 1}]}, ]
也许lodash有什么微妙的解决方案?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您可以使用Object.values 与 Array.prototype.reduce 结合() 和 Array.prototype .push()
代码:
你不需要lodash,你可以只使用JavaScript
使用forEach
使用减少和Object.values()
使用地图和减少
输出