假设有10个配置项,id是从1到10,数据库只存用户操作过的数据,一个id是一条数据
type | user_id | active |
---|
1 | 1 | 0 |
3 | 1 | 0 |
6 | 1 | 0 |
那么查询这个用户的数据库数据得到
const userSwitches = [
{"type": 1, "active": 0},
{"type": 3, "active": 0},
{"type": 6, "active": 0},
];
组装数据
const switches = Array.from({length: 10}, (v, index) => {
const type = index + 1;
return userSwitches.find(item => item.type === type)
|| {type, "active": 1},
});
就能得到数据
[
{"type": 1, "active": 0},
{"type": 2, "active": 1},
{"type": 3, "active": 0},
{"type": 4, "active": 1},
{"type": 5, "active": 1},
{"type": 6, "active": 0},
{"type": 7, "active": 1},
{"type": 8, "active": 1},
{"type": 9, "active": 1},
{"type": 10, "active": 1},
]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…