Jay
Jay

Reputation: 187

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])
                    }
                }

Upvotes: 0

Views: 259

Answers (2)

Bruno Pinheiro
Bruno Pinheiro

Reputation: 96

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.

Upvotes: 1

trincot
trincot

Reputation: 350272

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);

Upvotes: 1

Related Questions