reverse push order

Asked

Viewed 188 times

1

I’m using the push in a request to put a new element in a list, but I noticed that the push puts the last result down there. I want to reverse that.

I’m using v-for to list, but I believe that the "fix" will be in the same push...:

   dados.retornodosite.push(response.data);

How to invert the array?

2 answers

9


UNSHIFT

If you want you can use the method unshift. If you want to know more read here.

var nomes = ["João", "Maria", "André", "Marcia"];
nomes.unshift("Luís","Adriano");

console.log(nomes);

  • worked out!!! thank you very much!!

  • 1

    @Schedule If the answer solved your problem mark it as correct so that future visitors also take advantage of the solution.

1

Splice

If the object is an array, the push will insert at the end. To insert at the beginning you can use the splice

Example:

var final = 4;
var comeco = 1;

var arr = [2, 3];

arr.splice(0, 0, comeco);

console.log(arr);

arr.push(final);

console.log(arr);

Upshot:

// inicial
[2, 3] 

// depois do splice
[1, 2, 3]

// depois do push
[1, 2, 3, 4]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.