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
  • 17,074
  • 5
  • 83
  • 129

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
  • 11,517
  • 3
  • 23
  • 44