关于根据动态数量的对象的某属性的数组数量呈乘机式增长的数据处理

adta是原始数组,currentIndex默认是零,currentObject初始对象,result处理生成的结果

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);

你可能感兴趣的:(js,javascript)