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

112
Vistas
Grouping values into one array of objects from two objects based on differing keys

I need to group two objects into an array of objects.

Beginning objects:

{strIngredient1: 'Light rum', 
strIngredient2: 'Lime', 
strIngredient3: 'Sugar', 
strIngredient4: 'Mint', 
strIngredient5: 'Soda water'}

{strMeasure1: '2-3 oz ', 
strMeasure2: 'Juice of 1 ', 
strMeasure3: '2 tsp ', 
strMeasure4: '2-4 '}

I would like the final array of object to look like this:

[
   {ingredient: 'Light rum', measure: '2-3 oz'},
   {ingredient: 'Lime', measure: 'Juice of 1'},
   etc... (if no measure, fill with empty string)
]

I have tried parsing the objects into two arrays based on keys and values, then looping through both arrays and outputting the values based on the number in the keys, however there has to be a more efficient way of getting the desired outcome. Any suggestions?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can loop over the keys of the ingredients object and create the desired array by slicing out the ingredient number and looking for the corresponding measure for that number.

const 
  ingredients = { strIngredient1: "Light rum", strIngredient2: "Lime", strIngredient3: "Sugar", strIngredient4: "Mint", strIngredient5: "Soda water" },
  measurements = { strMeasure1: "2-3 oz", strMeasure2: "Juice of 1", strMeasure3: "2 tsp", strMeasure4: "2-4" },
  result = Object.keys(ingredients).map((k) => ({
    ingredient: ingredients[k],
    measure: measurements[`strMeasure${k.slice(13)}`] ?? "",
  }));

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could separate the keys and build a new object.

const
    data = [{ strIngredient1: 'Light rum', strIngredient2: 'Lime', strIngredient3: 'Sugar', strIngredient4: 'Mint', strIngredient5: 'Soda water' }, { strMeasure1: '2-3 oz ', strMeasure2: 'Juice of 1 ', strMeasure3: '2 tsp ', strMeasure4: '2-4 ' }],
    keys = { strIngredient: 'ingredient', strMeasure: 'measure' },
    result = data.reduce((r, o, i) => {
        Object.entries(o).forEach(([key, value]) => {
            const [, k, i] = key.match(/^(.*?)(\d+)$/);
            (r[i - 1] ??= { ingredient: '', measure: '' })[keys[k]] = value;
        });
        return r;        
    }, []);

console.log(result);

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