Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
Trying to iterate through an object's values and insert into new object (JS)

I am trying to create a series of new objects using the values from an existing object to push to my database.

Here is the existing object:

{
  name: 'Pasta',
  method: 'Cook pasta',
  ingredients: [
    { measure: 'tbsp', quantity: '1', ingredient_name: 'lemon' },
    { measure: 'g', quantity: '1', ingredient_name: 'salt' },
    { measure: 'packet', quantity: '1', ingredient_name: 'spaghetti' },
    { measure: 'litre', quantity: '1', ingredient_name: 'water' }
  ]
}

Basically I have a function that inserts and returns the id of the recipe into one table, then inserts and returns/or finds the ids of the relevant ingredients and the final part (with which I am struggling) is to combine the returned recipe_id, ingredient_id and the correct measure and quantity (as written in the object above).

Here is where I have gotten to:

//starting point is here
async function addNewRecipe(newRecipe, db = connection) {
  console.log(newRecipe)
  const recipeDetails = {
    recipe_name: newRecipe.name,
    recipe_method: newRecipe.method,
  }
  const ingredientsArray = newRecipe.ingredients

  const [{ id: recipeId }] = await db('recipes')
    .insert(recipeDetails)
    .returning('id')

  const ingredientsWithIds = await getIngredients(ingredientsArray) //returns an array of ids


  ingredientsWithIds.forEach((ingredientId) => {
    let ingredientRecipeObj = {
      recipe_id: recipeId, //works
      ingredient_id: ingredientId, //works
      measure: newRecipe.ingredients.measure, //not working - not sure how to match it with the relevant property in the newRecipe object above.
      quantity: newRecipe.ingredients.quantity,//not working - not sure how to match it with the relevant property in the newRecipe object above.
    }
    //this is where the db insertion will occur
  })
}

The desired output would be:

ingredientRecipeObj = {
      recipe_id: 1
      ingredient_id: 1
      measure: tbsp
      quantity: 1
} then insert this into db

followed by:
ingredientRecipeObj = {
     recipe_id: 1
     ingredient_id: 2
     measure: g
     quantity: 1
} then insert into db

etc. etc.
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem seems to be that the function "getIngredients" returns only the IDs. Once you have fetched them, you have no way of knowing which ID is for which ingredient. One way to change that is to make the method return an array of both the ID and the ingredient name. Then you could match them like this:

const ingredientsWithIds = await getIngredients(ingredientsArray) //now an array of objects with ingredient_name and id

ingredientsWithIds.forEach((ingredient) => {
    const recipeIngredient = ingredientsArray.find(ri => ri.ingredient_name === ingredient.ingredient_name)
    const ingredientRecipeObj = {
      recipe_id: recipeId,
      ingredient_id: ingredient.id, 
      measure: recipeIngredient.measure, 
      quantity: recipeIngredient.quantity,
    }
    //this is where the db insertion will occur
  })

Since you haven't posted the "getIngredients" function it is hard to say exactly how to adapt it to return the name as well.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda