I have an object structured as follows:
NewObjName: Object { OLDCOLUMNNAME1: "NEWCOLUMN_NAME1", OLDCOLUMNNAME2: "NEWCOLUMN_NAME2", OLDCOLUMNNAME3: "NEWCOLUMN_NAME3"}
Next, there is an array containing objects in this format:
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"}
]
Both objects share a common key = OLDCOLUMNNAME.
The goal is to change the key (OLDCOLUMNNAME) to NEWCOLUMN_NAME from the first object (NewObjName).
The desired output should be:
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"}
]
An attempt was made using the following code:
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]
}
})
})
})
However, when checking the result (console.log(transformed)), it returns undefined.