js数组对象某个值相同合并相同项在⼀个对象中
let data =[
{
DATE_T:"20211220"
DAY_WT:0.64
ITEM_ID:"JYD130018"
ITEM_NAME:"线材"
TYPE:"线材"
UNIT:"万吨"
UNIT_CODE:"EG"
},
{
DATE_T:"20211220"
DAY_WT:2.77
ITEM_ID:"JYD130018"
ITEM_NAME:"中厚板"
TYPE:"中厚板"
UNIT:"万吨"
UNIT_CODE:"EG"
},
{
DATE_T:"20211220"
DAY_WT:0.84
js合并两个数组ITEM_ID:"JYD130018"
ITEM_NAME:"棒材"
TYPE:"棒材"
UNIT:"万吨"
UNIT_CODE:"EG"
},
{
DATE_T:"20211220"
DAY_WT:0.53
ITEM_ID:"JYD130019"
ITEM_NAME:"线材"
TYPE:"线材"
UNIT:"万吨"
UNIT_CODE:"SG"
},
{
DATE_T:"20211220"
DAY_WT:0.4
ITEM_ID:"JYD130019"
ITEM_NAME:"中厚板"
TYPE:"中厚板"
UNIT:"万吨"
UNIT_CODE:"SG"
},
{
DATE_T:"20211220"
DAY_WT:1.32
ITEM_ID:"JYD130019"
ITEM_NAME:"直条螺纹钢"
TYPE:"直条螺纹钢"
UNIT:"万吨"
UNIT_CODE:"SG"
},
{
DATE_T:"20211220"
DAY_WT:1.17
ITEM_ID:"JYD130020"
ITEM_NAME:"线材"
TYPE:"线材"
UNIT:"万吨"
UNIT_CODE:"ZN"
UNIT_CODE:"ZN"
},
{
DATE_T:"20211220"
DAY_WT:3.17
ITEM_ID:"JYD130020"
ITEM_NAME:"中厚板"
TYPE:"中厚板"
UNIT:"万吨"
UNIT_CODE:"ZN"
},
{
DATE_T:"20211220"
DAY_WT:0.84
ITEM_ID:"JYD130020"
ITEM_NAME:"棒材"
TYPE:"棒材"
UNIT:"万吨"
UNIT_CODE:"ZN"
},
{
DATE_T:"20211220"
DAY_WT:1.32
ITEM_ID:"JYD130020"
ITEM_NAME:"直条螺纹钢"
TYPE:"直条螺纹钢"
UNIT:"万吨"
UNIT_CODE:"ZN"
},
]
let res=[],obj={},index=0;
data.forEach(item=>{
//Object的hasOwnProperty()⽅法返回⼀个布尔值,判断对象是否包含特定的⾃⾝(⾮继承)属性。//判断属性对象中是否存在这个相同key,若存在就赋值
if(obj.hasOwnProperty(item.ITEM_NAME)){
res[obj[item.ITEM_NAME]][item.UNIT_CODE]= item.DAY_WT ||0;
}else{
obj[item.ITEM_NAME]= index++;
res.push({
ITEM_NAME:item.ITEM_NAME,
UNIT:item.UNIT,
[item.UNIT_CODE]: item.DAY_WT ||0
})
}
})
结果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论