Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

how i can 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

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading