I've been searching for a solution online for about an hour now, but I haven't found the right answer yet. So, I decided to share the response I'm getting from the API with you:
[
{
"Name": "name1",
"Title": "Name One",
"Children": [
{
"Name": "Name 1.1",
"Title": "Name one point one"
},
]
Now, I need this data to fit into a specific format like this:
{
title: 'Name One',
value: 'name1',
key: '1',
children: [
{
title: 'Name one point one',
value: 'Name 1.1',
key: 'key1',
},
I have been trying to achieve this using a foreach loop, but it's not working as expected because I need to do all of this within one instance of a loop. Here is what I tried (using vue2):
created() {
getData().then(response => {
const formattedResponse = []
response.forEach((el, key) => {
formattedResponse.title = response.Title
formattedResponse.name = response.Name
formattedResponse.children = response.Children
})
})