【问题标题】:How to sort an array of object properties? or turn object properties upside down如何对对象属性数组进行排序?或颠倒对象属性
【发布时间】:2021-04-16 14:26:35
【问题描述】:

大家好,我遇到了一个问题,希望得到一些帮助……我在尝试复制这个对象数组时遇到了麻烦……

   {
  "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 ......

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

【问题讨论】:

    标签: javascript sql knex.js


    【解决方案1】:

    如果要添加到成分数组的开头,只需:

    ingredients.unshift(object)
    

    你也可以完全反转一个数组

    ingredients.reverse()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2021-10-21
      相关资源
      最近更新 更多