如下有一个数组对象:
const arr = [
{
secondType: "原料成本",
templateKey: "ratio",
templateSort: 0,
templateValue: '10%',
thirdType: "原料成本总计"
},
{
secondType: "原料成本",
templateKey: "cost",
templateSort: 0,
templateValue: "5",
thirdType: "原料成本总计"
},
{
secondType: "原料成本",
templateKey: "ratio",
templateSort: 1,
templateValue: null,
thirdType: "原料成本总计"
},
{
secondType: "原料成本",
templateKey: "cost",
templateSort: 1,
templateValue: "3",
thirdType: "原料成本总计"
}
]
需要组成一个新数组是这样:
const newArr = [
{
ratio:'10%',
cost:'5',
},
{
ratio:null,
cost:'3'
},
]
新数组newArr是以arr的templateKey作为自己的key,然后根据templateSort是否相同放在同一个对象,如
大家有什么好的办法