-3

I have an object like below

[
  {
   "day"  : "monday",
   "value": 1
   },
  {
   "day"  : "tuesday",
   "value": 2
   },
   ...
]

Is there any native javascript way to replace the key with a new key. Here I need to replace the "day" & "value" with "x" & "y" respectively. (like below)

[
  {
   "x"  : "monday",
   "y": 1
   },
  {
   "x"  : "tuesday",
   "y": 2
   },
   ...
]
Manu J
  • 133
  • 9
  • Object properties can be added and removed, but not really renamed... although the value can be assigned to a different property. – evolutionxbox Dec 16 '21 at 15:03
  • You cannot change property names. You can add new properties and delete old ones. – Pointy Dec 16 '21 at 15:04
  • 2
    Does this answer your question? [JavaScript: Object Rename Key](https://stackoverflow.com/questions/4647817/javascript-object-rename-key) – evolutionxbox Dec 16 '21 at 15:06
  • 1
    One basic approach could be to use `Object.entries` and then map all the properties and then do `Object.fromEntries` to get a *new* object. Is that usable or does it have to be the same object? (Because it's feasible to delete all properties of the old object and add in all the new properties if absolutely necessary, but I suspect it's likely not necessary.) – Wyck Dec 16 '21 at 15:07
  • 2
    You are trying to [map an array onto another array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). Simplest way `arr.map(o => ({x: o.day, y: o.value}))` – Ruan Mendes Dec 16 '21 at 15:19
  • 1
    Using the [solution from this answer](https://stackoverflow.com/a/45287523/227299), this can be made generic. `const propMap = {day: 'x', value: 'y'}; const output = arr.map(o => renameKeys(o, propMap))` – Ruan Mendes Dec 16 '21 at 15:29
  • Thanks @JuanMendes, that solves my query. – Manu J Dec 16 '21 at 15:34
  • I don't understand why this question is closed. I believe its clear. – Manu J Dec 16 '21 at 15:35
  • 2
    @ManuJ It's clear but you have not shown an attempt. We can't tell which part you're having difficulty with. Questions without an attempt will always be closed because answers will just do the work for you instead of answering a real programming question that is helpful to others, which is the point of SO, it's not just help one person at a time. See https://stackoverflow.com/help/how-to-ask. Also, a [search for `rename object keys javascript`](https://stackoverflow.com/search?q=rename+object+keys+javascript) would have at least got you started – Ruan Mendes Dec 16 '21 at 16:02

1 Answers1

1

you can use map and inside the call back create a new object and modify it as required. Then return this object

const data = [{
    "day": "monday",
    "value": 1
  },
  {
    "day": "tuesday",
    "value": 2
  }
];

const newData = data.map((item) => {
  const obj = {};
  for (let keys in item) {
    if (keys === 'day') {
      obj['x'] = item[keys]
    }
    if (keys === 'value') {
      obj['y'] = item[keys]
    }


  };
  return obj
});
console.log(newData)
brk
  • 48,835
  • 10
  • 56
  • 78