2016-06-20 71 views
0

如果有一個配置應用程序,其中用戶可以指定幾個字段來生成表單。我們也有一個將圖像疊加在一起的系統。現在我需要獲取用戶配置的所有可能選項。因此,當我從配置獲得的所有設置我得到這個陣列陣列數組陣列的組合函數

[ 
    [ 'prs', 'fctr', 'fcop' ], 
    [], 
    [ 'standaard', 'duimgat' ], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [ 'action barrel', 'only action' ], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    [], 
    ['x', 'y'], 
    [], 
    [] 
] 

我現在試圖實現的是,我得到的所有可能的組合從上面。所以對於第一組合排我試着去理解PRS/prs_standaard/prs_duimgat/prs_standaard_action桶/ prs_standaard_only_action/PRS _standaard_action barrel_x/prs_standaard_only action_ X/prs_standaard_action barrel_y/prs_standaard_only barrel_y等

空數組可以用數據來填充,以及。這可以由用戶自行添加。所以我嘗試找到組合功能。

目前我有這個,但它不能正常工作。

_createPossibleOptionsFromStackAbleImages (stackAbleImages) { 
    let result = []; 

    if (stackAbleImages[0] !== undefined) { 
     stackAbleImages[0].forEach((bmStockModelImages) => { 
     let currentIndex = result.length; 
     result.push(bmStockModelImages); 
     stackAbleImages.forEach((questionStackAbleImage, index) => { 
      if (index === 0) { return; } 
      let currentQuestionLength = questionStackAbleImage.length; 
      questionStackAbleImage.forEach((stackAbleImage) => { 
      result.push(`${result[currentIndex]}_${stackAbleImage}`); 
      }); 
      currentIndex += currentQuestionLength; 
     }); 
     }); 
    } 

    console.log(result); 

    return result; 
    } 

我只需要從上到下的組合。所以我不需要x_action_barrel等提前

+1

我會建議你看看遞歸。遍歷每個數組並挑選每個元素。對於第一個元素,轉到下一個數組並選取每個元素。所以只要有更多的數組需要迭代,就可以調用函數。完成後,返回一個遞歸步驟並轉到最後一個數組的下一個元素,依此類推。有關javascript和遞歸的stackoverflow的許多例子,如果你搜索它。 – Urknecht

+0

@Urknecht。當然,我忘記了遞歸。現在明白了,感謝提醒 –

回答

0

感謝@Urknecht的想法來使用遞歸。所以我最後的代碼是這樣的。 貸@Urknecht爲理念

static _createPossibleOptionsFromStackAbleImages (stackAbleImages) { 
    let result = []; 

    console.log(ConfiguratorPreLoads._makePossibleCombinations([], stackAbleImages[0], stackAbleImages, 0)); 

    return result; 
    } 

    static _makePossibleCombinations (previous, current, all, index, result = []) { 
    //All possible options are checked 
    if (index === all.length) { 
     return result; 
    } 

    //Init state 
    if (previous[0] === undefined) { 
     current.forEach((currentValue) => { 
     result.push(currentValue); 
     }); 
     ++index; 
     return ConfiguratorPreLoads._makePossibleCombinations(result, all[index], all, index, result); 
    } 

    //The array is empty or we are done 
    if (current === undefined || current[0] === undefined) { 
     ++index; 
     return ConfiguratorPreLoads._makePossibleCombinations(previous, all[index], all, index, result); 
    } 

    //Remember the combinedPreviousValues so we can build new one from them 
    let combinedPrevious = []; 

    previous.forEach((previousValue) => { 
     current.forEach((currentValue) => { 
     combinedPrevious.push(`${previousValue}_${currentValue}`); 
     result.push(`${previousValue}_${currentValue}`); 
     }); 
    }); 

    index++; 
    //Recursion 
    return ConfiguratorPreLoads._makePossibleCombinations(combinedPrevious, all[index], all, index, result); 
    } 
1

我認爲你可以從重組二維數組數據像這樣的樹,然後從上面分析到下(^_^)

   __________ root ____________ 
      /   |    \ 
      __prs   fctr__   _fcop___ 
     / \  / \  /  \ 
    standard duimgat standard duimgat standard duimgat 
/ \ / \ / \ / \ / \ / \ 
+0

感謝您的回覆。但是在目前的數據庫模型中,我很難 –