const _ = require('lodash');
const _ = require('lodash/core');
const data = {
one: {
section: [
{
name: 'This name one',
paramsOne: 1,
paramsTwo: 1,
childArrayOne: [
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
],
childArrayTwo: [
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
],
},
{
name: 'This name two',
paramsOne: 1,
paramsTwo: 1,
childArrayOne: [
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
],
childArrayTwo: [
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
],
}
]
},
two: {
section: [
{
name: 'This name one two',
paramsOne: 1,
paramsTwo: 1,
childArrayOne: [
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
],
childArrayTwo: [
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
],
},
{
name: 'This name two two',
paramsOne: 1,
paramsTwo: 1,
childArrayOne: [
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
],
childArrayTwo: [
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
],
}
]
},
};
getRowsForTable(data) {
const result = _.mapValues(data, (value, key) => {
if (!value?.sections) {
return;
}
const additionalRows = value.sections.map((val: any) => {
val.isTotal = true;
return [...val.miningAreas, ...val.childTotals];
});
return { rows: [...value.sections, ..._.flatMap(additionalRows)] };
});
return result;
}
}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
我使用lodash库,数据必须转换为输出到形式:
{
one: {
rows: [
{
name: 'This name one',
paramsOne: 1,
paramsTwo: 1,
total: true
},
{
name: 'Supp name one',
},
{
name: 'Supp name two',
},
{
name: 'This name two',
paramsOne: 1,
paramsTwo: 1,
total: true
},
{
name: 'Supp name one',
},
{
name: 'Supp name two',
}
]
},
two: {
rows: [
//Также как и в one
]
}
}
也就是说,我需要用行替换部分,用标记total:true扩展行中的主要对象,并且还要从childArrayOne(应该总是先行)childArrayTwo添加对象。