I have java script array of objects read from html table like below depId represent the row number, branchId represent column number and val it linked with check box (vue js v-model) 3*3 table data:
permissions= [
[{depId:1,branchId:1,val:true},{depId:1,branchId:2,val:true},{depId:1,branchId:3}],
[{depId:2,branchId:1},{depId:2,branchId:2},{depId:2,branchId:3}],
[{depId:3,branchId:1},{depId:3,branchId:2},{depId:3,branchId:3,val:true}]
]
I need to send this data to axios API, but the data should be in below format
data[0][branches][0]=1
data[0][branches][1]=2
data[0][department]=1
data[1][branches][0]=3
data[1][department]=3
I tried something like this but it have problems (the data sent in wrong indexes)
let data={};
permissions.forEach((row, i) => {
row.forEach((col, j) => {
if (col["val"] === true) {
data[`data[${i}][branches][${j}]`] = col.branchId;
data[`data[${i}][department]`] = col.deptId;
}
});
});
console.log(data);
how the loop should be to send the data in correct way?
the current result is
"data[0][branches][0]": 1,
"data[0][department]": 1,
"data[0][branches][1]": 2,
"data[2][branches][2]": 3,
"data[2][department]": 3
but it have problemswhat are the problems?data[${i}][branches][${j}]]" really the way you want to do the job? I don't even want to understand that one... it's bad readability for sure.