adta是原始数组,currentIndex默认是零,currentObject初始对象,result处理生成的结果
javascript
function generateObjects(data, currentIndex, currentObject, result) {
if (currentIndex === data.length) {
result.push(currentObject);
return;
}
const currentCode = data[currentIndex].code;
const currentSelect = data[currentIndex].select;
for (let i = 0; i < currentSelect.length; i++) {
const newObject = { ...currentObject };
newObject[currentCode] = currentSelect[i];
generateObjects(data, currentIndex + 1, newObject, result);
}
}
const result = [];
generateObjects(data, 0, {}, result);