I'm kinda new to VueJS so I was hoping to get some help. I'm currently returning an array of json objects from a PHP file.
Example:
<?php
/*
Returns an array similar to this:
[
{name: 'foo'},
{name: 'bar'},
{name: 'banana'}
]
*/
echo json_encode(array_values($array));
?>
And I'm appending this array of objects to an already existing array of objects in Vue:
axios.post('http://localhost/get_array.php').then(response => {
// Append returned array to already existing array
for (var i = 0; i <= response.data.length - 1; i++) {
this.existingArray.push(response.data[i])
}
}).catch(e => {
console.log("Error")
})
Right now I'm appending the data with a for loop but I was wondering if VueJS has an in-built function that does this automatically without having to use the for loop?
Array.prototype.concat()and Reactivity in depth on Vue docs.concat()to merge the arrays but it wouldn't update the view where I was rendering them. I also triedVue.set()but also couldn't get it to work, that's why I had to resort back to the for loop. @Nitconcat?this.existingArray.concat(response.data). Thanks everyone, I got it working withconcatnow and it's much easier than the for loop.