JS数组对应关系,通过不同参数进行组装JSON数组?
第一是后端返回的数据:需要处理成第二info的数据进行对应显示let obj = {
id: 4,
name: "水水",
Models: [
{
tip: "小样",
peak: "小王",
tipList: ["01", "02"], //小样
peakList: ["03", "04"], //小王
},
{
tip: "小李",
peak: "小峰",
tipList: ["01", "02"], //小李
peakList: ["03", "04"], //小峰
},
],
};
我需要处理成:
let info = {
id: 4,
name: "水水",
Models: [
{
dataTable:[
{
name: tip,//小样
dayTime:tipList//["01", "02"]
},{
name: peak,//小王
dayTime:peakList //["03", "04"]
}
]
},
{
dataTable:[
{
name: tip,//小李
dayTime:tipList//["01", "02"]
},{
name: peak,//小峰
dayTime:peakList //["03", "04"]
}
]
},
],
}; const info = {
...obj,
Models: obj.Models.map((model) => ({
dataTable: Object.entries(model)
.filter(([, v]) => !Array.isArray(v))
.map(() => ({ name: v, dayTime: model })),
})),
};
{
id: 4,
name: '水水',
Models: [
{
dataTable: [
{ name: '小样', dayTime: [ '01', '02' ] },
{ name: '小王', dayTime: [ '03', '04' ] }
]
},
{
dataTable: [
{ name: '小李', dayTime: [ '01', '02' ] },
{ name: '小峰', dayTime: [ '03', '04' ] }
]
}
]
}
页:
[1]