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

104
Vistas
Map an array of key/value objects of bigger length into other array

Hello developers Im trying to map the key value i have in an array of objects , into other array of minor length, but due to the task i have been committed is necessary to do it in this way.

Lets say i Have an array of objects of length 2 with this structure:

const array1=[
  { status: "ok", actual: true }, 
  { status: "not ok" }, 
]

And the array i pretend to add to this one has a bigger length and look like this:

const iconsStepper = [
        { icon: 'x' },
        { icon: 'xx' },
        { icon: 'xxx' }
];

Then the expected result , having in mind i need to couple the second array into the first would be :

[
  { status: "ok", actual: true ,icon:x}, 
  { status: "not ok",icon:xx }, 
  {icon:xxx }, 
]

I got a function mapping the second array into the first:

array1.map((object, index) => ({
      ...object,
      icon: iconsStepper[index].icon,
    }));

and following this logic the result thrown is :

[
  { status: "ok", actual: true ,icon:x}, 
  { status: "not ok",icon:xx }, 
]

Being omitted the last object of the iconSteeper array cause the length of the array it supposed should be mapped to is minor

How can i fix this problem without precisely invert the array to map. Thanks

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

0

You could mege the arrays by reducing them and get all property of the same indices at the same place.

const
    array1 = [{ status: "ok", actual: true }, { status: "not ok" }],
    array2 = [{ icon: 'x' }, { icon: 'xx' }, { icon: 'xxx' }],
    result = [array1, array2].reduce((r, a) => {
        for (let i = 0; i < a.length; i++) Object.assign(r[i] ??= {}, a[i]);
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

A slightly different approach with mapping the arrays.

const
    array1 = [{ status: "ok", actual: true }, { status: "not ok" }],
    array2 = [{ icon: 'x' }, { icon: 'xx' }, { icon: 'xxx' }],
    result = [array1, array2].reduce((r, a) => Object.assign(
        r,
        a.map((o, i) => ({...r[i], ...o }))
   ), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

const array1 = [
  { status: "ok", actual: true },
  { status: "not ok" },
];

const iconsStepper = [
  { icon: 'x' },
  { icon: 'xx' },
  { icon: 'xxx' },
];

const output = array1.length > iconsStepper.length ? array1.map((object, index) => {
  const returnObj = {
    ...object,
  }
  if (iconsStepper[index]) {
    returnObj['icon'] = iconsStepper[index].icon
  }
  return returnObj;
}) : iconsStepper.map((object, index) => ({
  ...array1[index],
  icon: iconsStepper[index].icon,
}));

console.log(output)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can combine Array#reduce() and Array#map() as follows:

const arr1 = [{status: "ok", actual: true },{ status:"not ok" }];

const arr2 = [{ icon: 'x' },{ icon: 'xx' },{ icon: 'xxx' }];

const result = [arr1, arr2].reduce(
    (acc,cur) => !acc.length ? cur : cur.map((e,i) => ({...acc[i],...e})), []
);

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