1 回答
TA贡献1877条经验 获得超6个赞
这并不漂亮,当您拥有非常大的数据集时,它的性能也不会很好,但它可以正常工作并产生您期望的输出。
const arr = [
{
"name":"D1D1",
"attritube":"HOST_NAME",
"value_inv":"TEST1",
"value_prod":"TESTA1"
},
{
"name":"D1D1",
"attritube":"HOST_NAME",
"value_inv":"TEST2",
"value_prod":"TESTB1"
},
{
"name":"D2D2",
"attritube":"COMMENTS",
"value_inv":"TEST1",
"value_prod":"TESTA1"
}
];
const newArr = _.chain(arr)
.groupBy('name')
.toPairs()
.map((val) => ({
name: val[0],
fields: _.map(val[1], (v) => _.omit(v, ['name']))
}))
.value();
console.log(newArr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
添加回答
举报