I have the following json returned from an ajax request
[
{
"id": "1",
"task": "eat pizza",
"username": "all"
},
{
"id": "2",
"task": "drink soda",
"username": "all"
}
]
I am trying to add specific pieces of data from the json to an existing js array.
this.$todoData2 = [
{
'id': '5',
'text': 'Cheese',
'done': false
},
]
I need to add json key id and its matching value – example: “id”: key,
I need to add json key task as text with its matching value – example: “text”: key
I need to add a “done”: “false” to set so a final result would look like:
this.$todoData2 = [
{
"id": "1",
"text": "eat pizza",
'done': false
}
{
"id": "2",
"text": "drink soda",
'done': false
}
{
'id': '5',
'text': 'Cheese',
'done': false
},
]
I have no examples of what I have tried as I am not sure where to even begin with this. Keep in mind that json may contain a lot more results.
3
Answers
Use
.forEach()
to iterate over the ‘updates’ Array, while destructuring the key / value pairs you want to useCreate your object, then
.push()
it to the ArrayYou can merge both arrays and then use
map
.If the point here is to simply append and rename keys, it may by done as simple as that: