-1

Guys Im a bit new to javascript. I have the following code. I need to assign a string name to the data object. How can I convert this object

const obj= { data: [
    { name: "subject", note: "note", time: "12:30-1:30" },
    { name: "subject", note: "note", time: "12:30-1:30" },
  ],}

as an example

const obj= { 'objectName': [
    { name: "subject", note: "note", time: "12:30-1:30" },
    { name: "subject", note: "note", time: "12:30-1:30" },
  ],}
  • Hello and welcome to Stack Overflow! Please take the [tour] and read through the [help], in particular [_How do I ask a good question?_](https://stackoverflow.com/help/how-to-ask) Do your [research](https://meta.stackoverflow.com/a/261593/6634591), [search](https://stackoverflow.com/help/searching) for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt, as an [edit] to your question, and say specifically where you're stuck. People will be glad to help. Good luck! – Luca Kiebel Jan 18 '22 at 10:15

1 Answers1

1

sadly you can't directly change the name of a key. But you can create a new one and delete the old one.

const obj= { data: [
    { name: "subject", note: "note", time: "12:30-1:30" },
    { name: "subject", note: "note", time: "12:30-1:30" },
  ]}

obj.newName = obj.data;
delete obj.data;

console.log(obj)
kevin
  • 872
  • 5
  • 18