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,
        },
]
0

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.