I'm currently working on a project using Angular. I have an index coming from the HTML, and here is the code snippet:
save(index){
//this method will be called on click of save button
}
In my component, I have an array structured like this:
data = [{
"name":"Rosy"
},
{
"name":"julia"
}]
Within the save method, I want to add a subarray for the index that I receive. For instance, if I receive index 0, I would like to add a subarray at the 0th index in the data array, resulting in:
data = [{
"name":"Rosy",
"Address":[{
"city":"London" // the city value is sourced from a component variable
}]
},
{
"name":"julia"
}]
If I receive index 0 again, with the city now being "Mumbai," the data will be updated as follows:
data = [{
"name":"Rosy",
"Address":[{
"city":"London"
},
{
"city": "Mumbai"
}
]
},
{
"name":"julia"
}]
This process is repeated for other indexes and elements. How can I achieve this?