Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!