1

Given this array of objects:

arrayBefore: [
        {
            a: "string1",
            b: 10,
        },
        {
            a: "string2",
            b: 20,
        },
]

how can one change the keys using JavaScript for the end result of:

arrayAfter: [
        {
            x: "string1",
            y: 10,
        },
        {
            x: "string2",
            y: 20,
        },
]
Emile Bergeron
  • 16,148
  • 4
  • 74
  • 121

1 Answers1

2

You can use Array.prototype.map to transform the objects.

const arrayBefore = [{
    a: "string1",
    b: 10,
  },
  {
    a: "string2",
    b: 20,
  },
]

const arrayAfter = arrayBefore.map((item) => ({
  x: item.a,
  y: item.b,
}))

console.log(arrayAfter)
Arun Kumar Mohan
  • 10,613
  • 3
  • 18
  • 40