Adicionar elemento no início do array com JavaScript

207d atrás

0 0

O método Array.unshift() permite adicionar itens no início de um array no #JavaScript, e retorno o tamanho atualizado do array.

Sintaxe

A função segue a seguinte sintaxe:

unshift(element1);
unshift(element1, ...elementN);

Exemplo

Você pode adicionar diversos itens de uma vez só com unshift, veja o exemplo abaixo:

const arr = [1, 2, 3];

arr.unshift(4); // [4, 1, 2, 3]

arr.unshift(5, 6, 7); // [5, 6, 7, 4, 1, 2, 3]

Referências

Para mais informações, acesse o site:

Array.prototype.unshift() - JavaScript | MDN

The unshift() method of Array instances adds the specified elements to the beginning of an array and returns the new length of the array.


Comentários (0)

0/512
Link