0

嘿,伙计们遇到了一个问题,需要一些帮助......我在尝试复制这个对象数组时遇到了麻烦......

   {
  "recipe_id" : 1,
  "recipe_name": "Spaghetti Bolognese",
  "created_at": "2021-01-01 08:23:19.120",
  "steps": [
    {
      "step_id": 11,
      "step_number": 1,
      "step_instructions": "Put a large saucepan on a medium heat",
      "ingredients": []
    },
    {
      "step_id": 12,
      "step_number": 2,
      "step_instructions": "Add 1 tbsp olive oil",
      "ingredients": [
        { "ingredient_id": 27, "ingredient_name": "olive oil", "quantity": 0.014 }
      ]
    },
  ]
}

我得到的结果是这样的

{
    "created_at": "2021-01-03 09:08:19.150",
    "recipe_id": 2,
    "recipe_name": "chicken nuggets",
    "steps": [
        {
            "ingredients": [],
            "step_id": 7,
            "step_instructions": "Go to backyard",
            "step_number": 1
        },
        {
            "ingredients": [],
            "step_id": 8,
            "step_instructions": "find a chicken",
            "step_number": 2
        },
        {
            "ingredients": [],
            "step_id": 9,
            "step_instructions": "take its nuggets",
            "step_number": 3
        },
        {
            "ingredients": [],
            "step_id": 10,
            "step_instructions": "season nuggets with spices",
            "step_number": 4
        },
        {
            "ingredients": [],
            "step_id": 10,
            "step_instructions": "season nuggets with spices",
            "step_number": 4
        },
        {
            "ingredients": [],
            "step_id": 11,
            "step_instructions": "cook for 30 minute till done",
            "step_number": 5
        }
    ]
}

我这样做的方式是这样的:

async function getRecipeById(recipeId){

  const recipe = await db('recipes as r')
    .join('steps as s','s.recipe_id','r.recipe_id')
    .leftJoin('step_ingredients as si', 'si.recipe_steps_id', 's.recipe_steps_id')
    .leftJoin('ingredients as i', 'i.ingredient_id', 'si.ingredient_id')
    .select('r.*','s.recipe_steps_id','s.recipe_steps_number','s.recipe_step_instructions','i.ingredient_id','i.ingredient_name','si.step_ingredient_quantity')
    .where('r.recipe_id',recipeId)


  const result = {
    recipe_id: recipe[0].recipe_id,
    recipe_name: recipe[0].recipe_name,
    created_at: recipe[0].createdAt,
    steps: []
  }

  let ingredients = []

  recipe.forEach(step => {

    if(step.recipe_steps_id){
      result.steps.push({
        step_id: step.recipe_steps_id,
        step_number: step.recipe_steps_number,
        step_instructions: step.recipe_step_instructions,
        ingredients: ingredients
      })
    }

  });

  return result

当我创建一种将成分应用到我的工作中的方法时,它最终会颠倒过来……

    "created_at": "2021-01-03 09:08:19.150",
    "recipe_id": 2,
    "recipe_name": "chicken nuggets",
    "steps": [
        {
            "ingredients": [
                {
                    "ingredient_id": 5,
                    "quantity": 50,
                    "step_number": "chicken"
                }
            ],
            "step_id": 7,
            "step_instructions": "Go to backyard",
            "step_number": 1
        },
        {
            "ingredients": [
                {
                    "ingredient_id": 5,
                    "quantity": 50,
                    "step_number": "chicken"
                }
            ],
            "step_id": 8,
            "step_instructions": "find a chicken",
            "step_number": 2
        },
        {
            "ingredients": [
                {
                    "ingredient_id": 8,
                    "quantity": 250,
                    "step_number": "nuggets"
                },
                {
                    "ingredient_id": 10,
                    "quantity": 50,
                    "step_number": "spices"
                }
            ],
            "step_id": 9,
            "step_instructions": "take its nuggets",
            "step_number": 3
        },
        {
            "ingredients": [
                {
                    "ingredient_id": 8,
                    "quantity": 250,
                    "step_number": "nuggets"
                },
                {
                    "ingredient_id": 10,
                    "quantity": 50,
                    "step_number": "spices"
                }
            ],
            "step_id": 10,
            "step_instructions": "season nuggets with spices",
            "step_number": 4
        },
        {
            "ingredients": [
                {
                    "ingredient_id": 8,
                    "quantity": 250,
                    "step_number": "nuggets"
                },
                {
                    "ingredient_id": 10,
                    "quantity": 50,
                    "step_number": "spices"
                }
            ],
            "step_id": 10,
            "step_instructions": "season nuggets with spices",
            "step_number": 4
        },

我添加了这段代码来将成分推入一个数组,如果没有成分名称,它将返回一个空数组

if(step.ingredient_name && step.ingredient_name !== null){
      ingredients.push({
        ingredient_id: step.ingredient_id,
        step_number: step.ingredient_name,
        quantity: step.step_ingredient_quantity
      })
    }else{
      ingredients = []
    }

我的问题是我如何才能在很大程度上扭转结果被打印出来的方式......在 steps 数组中。我可以看到它是按字母顺序排列的,我想让它看起来像上面的示例所示...我正在为此使用 javascript 并使用 knex 构建一个 DB 以及为 DB 使用 SQL ....

同时,如果你能帮助我的代码,如果它为空,我没有像鸡肉一样打印两次成分,如果你看到的话

4

1 回答 1

0

如果要添加到成分数组的开头,只需执行以下操作:

ingredients.unshift(object)

您还可以使用完全反转数组

ingredients.reverse()

但是,如果您想按某个属性对对象进行排序,请查看 JavaScriptsort方法。你可以在这里阅读:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

于 2021-04-16T06:26:35.627 回答