adta是原始数组,currentIndex默认是零,currentObject初始对象,result处理生成的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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);
|