VueJs - Prepend to array
12,971
Solution 1
Unshift:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
this.number.unshift({number: 1});
You can also pass multiple arguments to add them all:
this.number.unshift({number: 1}, {number: 2});
The return value is the new length of the array:
var foo = [1];
var bar = foo.unshift(2, 3, 4);
//foo = [2, 3, 4, 1]; bar = 4;
Solution 2
Vue wraps an observed only this Array methods: push, pop, shift, unshift, splice, sort, reverse
. You can use unshift or splice. For example:
youArray.splice(0, 0, 'first item in Array');
Author by
Ricky Barnett
Updated on July 07, 2022Comments
-
Ricky Barnett almost 2 years
I'm trying to prepend data to an array in VueJs:
number: [ ], this.number.push({ number: 1 })
How do I prepend rather than append?