0

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"
});
VA1267
  • 145
  • 7
  • 1
    [computed property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names) – ASDFGerte Jan 18 '22 at 15:24

0 Answers0