I have the following small program to replace keys based on a condition.
Why do I need to use [] in renameKeys function to return a new object? I understand if I omit [] , it just prints
[{
newKey: "A"
}, {
newKey: "B"
}, {
newKey: "C"
}]
What is the significance of array notation [] ?Is there an alternate way as I am fairly new to javascript?
const renameKeys = (obj, keyMap) => {
const newArray = Object.keys(obj).map((key) => {
const newKey = keyMap[key] || key;
return {[newKey] : obj[key]};
});
console.log(newArray);
};
const obj = { name: "A", job: "B", school: "C" };
const result = renameKeys(obj, {
job: "NM",
name: "Name"
});