Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda