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

211
Views
Change key in array of objects having common key

I have an object like this:

NewObjName: Object { OLDCOLUMNNAME1: "NEWCOLUMN_NAME1", OLDCOLUMNNAME2: "NEWCOLUMN_NAME2", OLDCOLUMNNAME3: "NEWCOLUMN_NAME3"}

Then I have an array of objects like this:

array: [
1: Object { OLDCOLUMNNAME1: "VALUE", OLDCOLUMNNAME2: "VALUE", OLDCOLUMNNAME3: "VALUE"}
2: Object { OLDCOLUMNNAME1: "VALUE", OLDCOLUMNNAME2: "VALUE", OLDCOLUMNNAME3: "VALUE"}
3: Object { OLDCOLUMNNAME1: "VALUE", OLDCOLUMNNAME2: "VALUE", OLDCOLUMNNAME3: "VALUE"}
]

In both objects I have common key = OLDCOLUMNNAME.

how can I change key(OLDCOLUMNNAME) to NEWCOLUMN_NAME from first object (NewObjName)?

I need something like this:

array: [
1: Object { NEWCOLUMN_NAME1: "VALUE", NEWCOLUMN_NAME2: "VALUE", NEWCOLUMN_NAME3: "VALUE"}
2: Object { NEWCOLUMN_NAME1: "VALUE", NEWCOLUMN_NAME2: "VALUE", NEWCOLUMN_NAME3: "VALUE"}
3: Object { NEWCOLUMN_NAME1: "VALUE", NEWCOLUMN_NAME2: "VALUE", NEWCOLUMN_NAME3: "VALUE"}
]

I just try to make this:

const transformed = array.map(x => {
            // console.log(x); // loop through array with objects
            Object.keys(x).map(key1 => {
            // console.log(key1); // get OLDCOLUMNNAME

                Object.keys(NewObjName).map(key => {
                // console.log(key); // get OLDCOLUMNNAME from NewObjName
                // console.log(NewObjName[key]) //  NEWCOLUMN_NAME

                if (key1 === key) {
                   //if true i just try to set into OLDCOLUMNNAME to NEWCOLUMN_NAME
                   key1 = NewObjName[key]
                   delete NewObjName[key]
                }
         })
    })
})

As a result of (console.log(transformed)) - I get undefined.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use reduce and forEach for the object we translate

const transformed  = array.reduce((acc, cur) => {
  const obj = {}
  Object.entries(cur).forEach(([key, val]) => { // take each item 
    obj[newObjName[key]] = val;                 // translate the key from newObjName lookup table
  })
  acc.push(obj)
  return acc;
}, [])
console.log(transformed)
<script>
const newObjName = {
  OLDCOLUMNNAME1: "NEWCOLUMN_NAME1",
  OLDCOLUMNNAME2: "NEWCOLUMN_NAME2",
  OLDCOLUMNNAME3: "NEWCOLUMN_NAME3"
}

const array = [{
    OLDCOLUMNNAME1: "VALUE1",
    OLDCOLUMNNAME2: "VALUE2",
    OLDCOLUMNNAME3: "VALUE3"
  },
  {
    OLDCOLUMNNAME1: "VALUE11",
    OLDCOLUMNNAME2: "VALUE22",
    OLDCOLUMNNAME3: "VALUE33"
  },
  {
    OLDCOLUMNNAME1: "VALUE111",
    OLDCOLUMNNAME2: "VALUE222",
    OLDCOLUMNNAME3: "VALUE333"
  }
]
</script>

about 4 years ago · Juan Pablo Isaza Report

0

This is probably how I'd tackle it, as a generic solution:

const arr = [
    { OLDCOLUMNNAME1: 'VALUE', OLDCOLUMNNAME2: 'VALUE', OLDCOLUMNNAME3: 'VALUE' },
    { OLDCOLUMNNAME1: 'VALUE', OLDCOLUMNNAME2: 'VALUE', OLDCOLUMNNAME3: 'VALUE' },
    { OLDCOLUMNNAME1: 'VALUE', OLDCOLUMNNAME2: 'VALUE', OLDCOLUMNNAME3: 'VALUE' },
  ];
  
// define my map: what I what my old column name is and what I what I want my new column name to be
  const map = {
    OLDCOLUMNNAME1: 'NEWCOLUMN_NAME1',
    OLDCOLUMNNAME2: 'NEWCOLUMN_NAME2',
    OLDCOLUMNNAME3: 'NEWCOLUMN_NAME3'
  };
  
//function to replace the  old column name, with the new column name as defined in the map
const replaceOldColumnNames = (obj, map) => Object.keys(obj)
    .reduce((r, c) => Object.assign(r, { [map[c]]: obj[c] }), {});
  
//iterate over the arr mapping out the old column names to the new ones using the above function
const newArr = arr.map(o => replaceOldColumnNames(o, map));

console.log(newArr)

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!