-10

I have a JSON object whose key's are renamed with the help of map() function. I am not understanding the working of it. Can someone explain how the key names are renamed?

This is my code for renaming JSON keys:

let arr = [
  {
    STUDENT_ID: "1",
    STUDENT_NAME: "abc"
  },
  {
    STUDENT_ID: "2",
    STUDENT_NAME: "xyz"
  }
]

let renamedArr = arr
  .map(({ STUDENT_ID: studentId, STUDENT_NAME: studentName }) => ({
    studentId,
    studentName
  }));

console.log({ renamedArr })

This is my output:

[
  {
    "studentId": "1",
    "studentName": "abc"
  },
  {
    "studentId": "2",
    "studentName": "xyz"
  }
]
Peter Seliger
  • 8,830
  • 2
  • 27
  • 33
Renee
  • 1
  • 3
  • 7
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#new_notations_in_ecmascript_2015 – jonrsharpe Apr 05 '22 at 13:20
  • 3
    It is less about the `.map()` and more about de-structuring. The exact code where the names are changed is this: `{ STUDENT_ID: studentId, STUDENT_NAME: studentName }`. Have you had an opportunity to search the documentation/internet for `de-structuring` - if not, please do give it a try. – jsN00b Apr 05 '22 at 13:21
  • 1
    Does this answer your question? [What does this symbol mean in JavaScript?](https://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript) and [What is the concept of Array.map?](https://stackoverflow.com/questions/17367889/what-is-the-concept-of-array-map) – Ivar Apr 05 '22 at 13:26
  • 1
    @jsN00b Yeah I looked into it and it makes sense now. Thank you. – Renee Apr 06 '22 at 04:50

1 Answers1

-4

i just thing of it as "convert these items in an array to those items in a new array". so like you could uppercase all items with ucArr = arr.map(s => s.toUpperCase())

it basically just runs "some operation" on each item in an array.

chovy
  • 65,853
  • 48
  • 201
  • 247