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

161
Vistas
How can I merge two arrays with the type and key value in ES6

I have two arrays like below:

const array1 = [
  { type: 'BA', value: 100 },
  { type: 'CA', value: 200 },
];
const array2 = [
  {
    NameBA: 'STANDARD',
    NameDescriptionBA: 'STANDARD',
    AgeBA: 0,
    NameCA: 'STANDARD',
    NameDescriptionCA: 'STANDARD',
    AgeCA: 0,
  },
]

The array1 has the type value 'BA', I have to find the array2 keys last 2 characters includes with the array1 type value 'BA' and merge to the related object of array1.

My expected result is

cosnt array1 = [
  { 
    type: 'BA', 
    value: 100,
    NameBA: 'STANDARD',
    NameDescriptionBA: 'STANDARD',
    AgeBA: 0,
  },
  { 
    type: 'CA',
    value: 200,
    NameCA: 'STANDARD',
    NameDescriptionCA: 'STANDARD',
    AgeCA: 0,
  },
]

I tried the below method but the output is incorrect

const keys = Object.keys(array2[0]);
array1.map(a => {
  keys.map(b => {
    if (b.includes(a.type)) {
      array2.map(c => {
        a.b = c.b
      })
    }
  });
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You're only using one object from the second array, so just changing that definition to be an object instead, you can convert the object to key/value pairs, and filter() them down to match the type:

const array = [
  { type: 'BA', value: 100 },
  { type: 'CA', value: 200 },
];

const object = {
  NameBA: 'STANDARD',
  NameDescriptionBA: 'STANDARD',
  AgeBA: 0,
  NameCA: 'STANDARD',
  NameDescriptionCA: 'STANDARD',
  AgeCA: 0,
};

const result = array.map(({type, value}) => ({
  type,
  value,
  ...Object.fromEntries(
    Object.entries(object).filter(([k, v]) => k.endsWith(type))
  )
}));

console.log(result);


If your second array is indeed an array with multiple objects, just throw in a flatMap() to flatten all the object entries:

const array1 = [
  { type: 'BA', value: 100 },
  { type: 'CA', value: 200 },
];

const array2 = [{
  NameBA: 'STANDARD',
  NameDescriptionBA: 'STANDARD',
  AgeBA: 0,
  NameCA: 'STANDARD',
  NameDescriptionCA: 'STANDARD',
  AgeCA: 0,
}];

const result = array1.map(({type, value}) => ({
  type,
  value,
  ...Object.fromEntries(
    array2.flatMap(Object.entries).filter(([k, v]) => k.endsWith(type))
  )
}));

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do something like this.

const array1 = [
  { type: 'BA', value: 100 },
  { type: 'CA', value: 200 },
];
const array2 = [
  {
    NameBA: 'STANDARD',
    NameDescriptionBA: 'STANDARD',
    AgeBA: 0,
    NameCA: 'STANDARD',
    NameDescriptionCA: 'STANDARD',
    AgeCA: 0,
  },
]

const keys = Object.keys(array2[0]);
const result = array1.map(a => {
  let currentObject = {}
  keys.map(b => {
    if (b.indexOf(a.type) !== -1) {
      currentObject = {
         ...currentObject,
         ...a, 
         [b]: array2[0][b]
      }
    }
  });
  return currentObject;
});

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