Search code examples
vue.jsvuejs2

Vue2 best way to update entire array


In Vue2, What is the best way to update the entire observed array and trigger a rerender in vue.js without having to iterate an push all the items?

I have read the documentation here: https://v2.vuejs.org/v2/guide/list.html#Replacing-an-Array

But it doesn't make sense to me having to filter, concat or slice just to update the array? There must be something I missed?


Solution

  • As suggested I will repond to this myself to help anyone in the same predicament.

    oldArr = newArr
    

    should still work (thank you choasia. Even though it is somewhat unclear in the vue.js documentation. Replacing the entire array will not fall under the caveats mentioned there (thank you Roy J ).

    One option might also be to to empty the array and then push the new one like:

    yourArray.push(... yourNewArray)
    

    Thank you reiner