2021-06-15 00:49:18 -07:00
---
id: 56bbb991ad1ed5201cd392ce
2021-07-21 20:53:20 +05:30
title: Manipular arrays com unshift()
2021-06-15 00:49:18 -07:00
challengeType: 1
videoUrl: 'https://scrimba.com/c/ckNDESv'
forumTopicId: 18239
dashedName: manipulate-arrays-with-unshift
---
# --description--
2021-07-30 01:41:44 +09:00
Você pode não apenas usar `shift` para remover elementos do início de um array, como também pode usar `unshift` para adicionar elementos ao início de um array, ou seja, adicionar elementos na posição inicial do array.
2021-06-15 00:49:18 -07:00
2021-07-30 01:41:44 +09:00
`.unshift()` funciona exatamente como `.push()` , mas, ao invés de adicionar o elemento ao final do array, `unshift()` adiciona o elemento no início do array.
2021-06-15 00:49:18 -07:00
2021-07-14 21:02:51 +05:30
Exemplo:
2021-06-15 00:49:18 -07:00
```js
2021-11-03 08:22:32 -07:00
const ourArray = ["Stimpson", "J", "cat"];
2021-06-15 00:49:18 -07:00
ourArray.shift();
ourArray.unshift("Happy");
```
2021-07-14 21:02:51 +05:30
Após o `shift` , `ourArray` teria o valor `["J","cat"]` . Após o `unshift` , `ourArray` teria o valor `["Happy","J","cat"]` .
2021-06-15 00:49:18 -07:00
# --instructions--
2021-11-03 08:22:32 -07:00
Adicione `["Paul", 35]` ao início da variável `myArray` usando `unshift()` .
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-14 21:02:51 +05:30
`myArray` agora deve ter `[["Paul", 35], ["dog", 3]]` .
2021-06-15 00:49:18 -07:00
```js
assert(
(function (d) {
if (
typeof d[0] === 'object' & &
d[0][0] == 'Paul' & &
d[0][1] === 35 & &
d[1][0] != undefined & &
d[1][0] == 'dog' & &
d[1][1] != undefined & &
d[1][1] == 3
) {
return true;
} else {
return false;
}
})(myArray)
);
```
# --seed--
## --after-user-code--
```js
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
```
## --seed-contents--
```js
// Setup
2021-11-03 08:22:32 -07:00
const myArray = [["John", 23], ["dog", 3]];
2021-06-15 00:49:18 -07:00
myArray.shift();
// Only change code below this line
2021-11-03 08:22:32 -07:00
2021-06-15 00:49:18 -07:00
```
# --solutions--
```js
2021-11-03 08:22:32 -07:00
const myArray = [["John", 23], ["dog", 3]];
2021-06-15 00:49:18 -07:00
myArray.shift();
myArray.unshift(["Paul", 35]);
```