簡體   English   中英

NodeJS:創建 api,它將設法根據鍵值動態生成 for 循環

[英]NodeJS: Create api that will manage to generate the for loop as dynamic based on key value

我有一個 MySQL 表,如下所示:

id   product_id  option_type
-------------------------------
14       6           2
15       6           1
16       6           1
17       6           2
18       6           2

在上述 sql 表的Sequelize ORM的幫助下應用 sql 查詢呈現以下對象數組和option_type鍵我需要生成基於option_type值的集合組合:

var records = [
{ id: 14, value: "M", product_id: 6, option_type: 2 },
{ id: 15, value: "White", product_id: 6, option_type: 1 },
{ id: 16, value: "Black", product_id: 6, option_type: 1 },
{ id: 17, value: "S", product_id: 6, option_type: 2 },
{ id: 18, value: "L", product_id: 6, option_type: 2 }]

我已經使用Array.prototype.filter()方法來創建組合。

const option_type_1 = records.filter((el) => { return el.option_type == 1 });
const option_type_2 = records.filter((el) => { return el.option_type == 2 });


for(const opt1 of option_type_1) {
  for(const opt2 of option_type_2) {
    const options = {
      "opt_one_id": opt1.id,
      "opt_two_id": opt2.id,
    };
    optionList.push(options)
  }
}

並得到如下回應......

{
  "data": [
  { "opt_one_id": 15, "opt_one_value": White, "opt_two_id": 14, "opt_two_value": M },
  { "opt_one_id": 15, "opt_one_value": White, "opt_two_id": 17,"opt_two_value": S },
  ...and so on
]}

現在,如何在添加更多 option_type 時動態管理它。

例如。 現在我們正在考慮 option_type = 1 和 option_type = 2,但將來如果再添加一個選項,那么 option_type = 3 也會出現在集合中。

那么,在那種情況下,如何使用鍵名來管理這個集合,那么到時候它會變成這樣

{ "opt_one_id": 15, "opt_two_id": 14, "opt_three_id": 17 },

因此,添加或刪除任何option_type都會對其進行相應管理。

提前致謝 !!!

=== 更新了 ===

考慮這個 option_type 的基表

id   option_type
-----------------
1     Colour
2     Size

因此,相應地它將生成如下組合

15 (White) * 14 (M)
15 (White) * 17 (S)
15 (White) * 18 (L)
16 (Black) * 14 (M)
16 (Black) * 17 (S)
16 (Black) * 18 (L)

您可以使用option_type使用下划線對結果進行分組,如下所示。

const options = _.groupBy(records, record => record.option_type);

console.log(options);

分組后,您將獲得以下結構。(考慮我們現在有第三個選項類型)

options = {
    
    1: [
         {id: 15, product_id: 6, option_type: 1}, 
         {id: 16, product_id: 6, option_type: 1}
       ],

    2: [
          {id: 14, product_id: 6, option_type: 2}, 
          {id: 17, product_id: 6, option_type: 2 },
          {id: 18, product_id: 6, option_type: 2 }
       ],
   3: [
          {id: 19, product_id: 6, option_type: 3}, 
      ]
    
    }

之后,您可以結合使用for..infor循環來獲得所需的結果,如下所示

const keys = Object.keys(options);
const rootItem = options[keys[0]];
delete options[keys[0]];
const finalResults = [];

for(let i =0; i< rootItem.length; i++) {
    for (option in options) {
        for (let j = 0; j<options[option].length; j++) {
            let item = {};
            item[`option_type_${rootItem[i].option_type}`] = rootItem[i].id;
            item[`option_type_${options[option][j].option_type}`] = options[option][j].id;
            finalResults.push(item);
        }
    }
} 

console.log(finalResults)  

[ { option_type_1: 15, option_type_2: 14 },
  { option_type_1: 15, option_type_2: 17 },
  { option_type_1: 15, option_type_2: 18 },
  { option_type_1: 15, option_type_3: 19 },
  { option_type_1: 16, option_type_2: 14 },
  { option_type_1: 16, option_type_2: 17 },
  { option_type_1: 16, option_type_2: 18 },
  { option_type_1: 16, option_type_3: 19 } ] 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM