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