1 回答
TA贡献1797条经验 获得超4个赞
您在对象中忘记了几个逗号permissions。下一个问题是您试图检查departmentIdin permissions,但它实际上就depId在那里。接下来是您不需要定义和跟踪iand j,它们在函数中作为传递给运行函数的第二个参数方便地提供给您forEach。
这是您要实现的目标的工作版本:
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
}]
]
let data = {};
let j = 0;
permissions.forEach((row) => {
let i = 0;
let departmentSeen = false;
row.forEach((col) => {
if (col["val"] === true) {
data[`data[${j}][branches][${i}]`] = col.branchId;
data[`data[${j}][department]`] = col.depId;
i++;
departmentSeen = true;
}
});
if (departmentSeen) {
j++;
}
});
console.log(data);
添加回答
举报