根据一个数组去补充其他数据的对应位置的数据内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let rerfer=[{id:'1',value:1},{id:'2',value:2},{id:'3',value:3},{id:'4',value:4},{id:'5',value:5},{id:'6',value:6},{id:'7',value:7},{id:'8',value:8},{id:'9',value:9},{id:'10',value:10},{id:'11',value:11},{id:'12',value:12}];
let studioInfo=
[
{studio:"CDU",group:[{name:"test1"},{name:"test2"},{name:"test3"},{name:"test4"}]},
{studio:"SHA",group:[{name:"stest1"},{name:"stest2"},{name:"stest3"},{name:"stest4"}]},
{studio:"XIY",group:[{name:"xtest1"},{name:"xtest2"},{name:"xtest3"},{name:"xtest4"}]}
]
let data = {
persons:[
{studio:"CDU",groupBooks:[
{name:"test1",books:[{id:'3',value:3},{id:'4',value:4},{id:'5',value:5}]},
{name:"test2",books:[{id:'1',value:1},{id:'4',value:4},{id:'5',value:5}]},
{name:"test3",books:[{id:'2',value:2},{id:'4',value:4},{id:'5',value:5}]},
{name:"test4",books:[{id:'3',value:3},{id:'4',value:4},{id:'8',value:8}]},
]},
{studio:"SHA",groupBooks:[
{name:"stest1",books:[{id:'3',value:3},{id:'4',value:4},{id:'5',value:5}]},
{name:"stest2",books:[{id:'1',value:1},{id:'4',value:4},{id:'5',value:5}]},
{name:"stest3",books:[{id:'2',value:2},{id:'4',value:4},{id:'5',value:5}]},
{name:"stest4",books:[{id:'3',value:3},{id:'4',value:4},{id:'8',value:8}]},
]},
{studio:"XIY",groupBooks:[
{name:"xtest1",books:[{id:'3',value:3},{id:'5',value:5}]},
{name:"xtest2",books:[{id:'1',value:1},{id:'4',value:4}]},
{name:"xtest3",books:[]},
{name:"xtest4",books:[{id:'3',value:3},{id:'4',value:4},{id:'8',value:8}]},
]},
],
softBookNumbers:[
{studio:"CDU",soft:[{id:'3',value:3,fte:5},{id:'5',value:5,fte:5}]}
]
}
//补充数据用的方法,可定义一个补充参数入参
function modify(rerList,modList,inputValue){
rerList.map(
(el,index)=>{
let params = new Object();
params={...inputValue};
params.id = el.id;
// params
if (el.id !== modList[index]?.id){
modList.splice(index,0,params);
}
}
)
}
//处理数据中不同属性下面的数组
function getList(studio,group,propertites){
for (let index = 0; index < data[propertites].length; index++) {
const element = data[propertites][index];
//遍历层数可由此控制;
if(studio=== element.studio){
//处理persons
if(propertites === 'persons')
{
let group1=[...element.groupBooks];
check(group1,group,"books",{id:'',value:0});
}
//处理softbooks
if(propertites === 'softBookNumbers'){
let soft = element.soft;
modify(rerfer,soft,{id:'',value:0,fte:0})
}
}
}
}
//用于处理含有group层的数据,及包含多层的数据
function check(groupList,group,propertites,inputValue) {
groupList.filter(
element=>{
if (element?.name === group) {
modify(rerfer,element[propertites],inputValue);
}
}
)
}
//最外层的数据
function test() {
//获取需要修改的数据
let count=0;
for (let index = 0; index < studioInfo.length; index++) {
let element = studioInfo[index];
for (let ind = 0; ind < element.group.length; ind++) {
//得到studio以及group
//处理persons,具体放在哪里数据所在的节点决定
getList(element.studio,element.group[ind].name,"persons");
count++;
}
//处理softbooks,具体放在哪里数据所在的节点决定
getList(element.studio,'',"softBookNumbers");
}
console.log(count)
}
test()
console.log(data);
</script>
</body>
</html>

浙公网安备 33010602011771号