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

189
Vistas
Merge two Object based on Object one value with Object two key

Want to merge object key values with another object value.

Suppose results object key should match with headers object key_name match because header should be CamelCase. I have headers object is:

let headers = [
  { key_name: 'pid', header_value: 'PID' },
  {
    key_name: 'card_no',
    header_value: 'Card Number'
  },
  {

    key_name: 'trans_id',
    header_value: 'Transaction ID'
  },
  {
    key_name: 'card_name',
    header_value: 'Card Name'
  }
]

results Object is:

let results = let results = [
  {
    pid: '1234567890',
    card_no: '546.......3742',
    trans_id: '2019124453159',
    card_name: 'Mastercard',
    code: '$'
  },
  {
    pid: '1234567890',
    card_no: '546.......3742',
    trans_id: '2019120534555',
    card_name: 'Visa',
    code: 'INR'
  }
]

Header object key_name matching values need to filter in final results Object like below. expected results is:

let results = [
  {
    PID: '1234567890',
    Card Number: '546.......3742',
    Transaction ID: '2019124453159',
    Card Name: 'Mastercard'
  },
  {
    PID: '1234567890',
    Card Number: '546.......3742',
    Transaction ID: '2019120534555',
    Card Name: 'Visa'
  }
]

I tried with below script its returning all values only:

for (let index = 0; index < results.length; index++) {
for (let i = 0; i < headers.length; i++) {

console.log(results [index][header[i].key_name])
                    }
                }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I would suggest turning the first object into a map, so you have faster look-up. Then turn your objects into key/value pairs with Object.entries, perform the mapping and the filtering, and then turn those pairs back to an object with Object.fromEntries

let headers = [{ key_name: 'pid', header_value: 'PID' },{key_name: 'card_no',header_value: 'Card Number'},{key_name: 'trans_id',header_value: 'Transaction ID'},{key_name: 'card_name',header_value: 'Card Name'}];

// Convert to map:
let map = new Map(headers.map(({key_name, header_value}) => [key_name, header_value]));

let arr = [{pid: '1234567890',card_no: '546.......3742',trans_id: '2019124453159',card_name: 'Mastercard',code: '$'},{pid: '1234567890', card_no: '546.......3742',trans_id: '2019120534555',card_name: 'Visa',code: 'INR'}];

// Use map to make transition
let result = arr.map(obj => 
    Object.fromEntries(Object.entries(obj).map(([key, value]) => 
        map.has(key) && [map.get(key), value]
    ).filter(Boolean))
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could look into the Object.keys(..) of the results array so you can map the old key values to the new ones. You could use something like this:

  const mappedHeaders = results.map((r) => {
    const keys = Object.keys(r);
    let newObj = {};
    for (const key of keys) {
      const newHeader = headers.find((h) => h.key_name === key);
      if (newHeader) newObj[newHeader.header_value] = r[key];
    }
    return newObj;
  });

You may need to check if the mapping exists and handle such cases to avoid errors.

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