2021-02-06 04:42:36 +00:00
---
id: 56bbb991ad1ed5201cd392ce
2021-03-16 08:41:19 -06:00
title: Manipula arreglos con unshift()
2021-02-06 04:42:36 +00:00
challengeType: 1
videoUrl: 'https://scrimba.com/c/ckNDESv'
forumTopicId: 18239
dashedName: manipulate-arrays-with-unshift
---
# --description--
2021-03-16 08:41:19 -06:00
No solo puedes desplazar (`shift` ) elementos del comienzo de un arreglo, también puedes des-desplazar (`unshift` ) elementos al comienzo de un arreglo. Por ejemplo añadir elementos delante del arreglo.
2021-02-06 04:42:36 +00:00
2021-03-16 08:41:19 -06:00
`.unshift()` funciona exactamente como `.push()` , pero en lugar de añadir el elemento al final del arreglo, `unshift()` añade el elemento al principio del arreglo.
2021-02-06 04:42:36 +00:00
2021-03-16 08:41:19 -06:00
Ejemplo:
2021-02-06 04:42:36 +00:00
```js
2021-10-31 23:08:44 -07:00
const ourArray = ["Stimpson", "J", "cat"];
2021-03-16 08:41:19 -06:00
ourArray.shift();
2021-02-06 04:42:36 +00:00
ourArray.unshift("Happy");
```
2021-03-16 08:41:19 -06:00
Después del `shift` , `ourArray` tendrá el valor `["J", "cat"]` . Después del `unshift` , `ourArray` tendrá el valor `["Happy", "J", "cat"]` .
2021-02-06 04:42:36 +00:00
# --instructions--
2021-10-31 23:08:44 -07:00
Agrega `["Paul", 35]` al principio de la variable `myArray` usando `unshift()` .
2021-02-06 04:42:36 +00:00
# --hints--
2021-03-16 08:41:19 -06:00
`myArray` debe contener ahora `[["Paul", 35], ["dog", 3]]` .
2021-02-06 04:42:36 +00: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-10-31 23:08:44 -07:00
const myArray = [["John", 23], ["dog", 3]];
2021-02-06 04:42:36 +00:00
myArray.shift();
// Only change code below this line
2021-10-31 23:08:44 -07:00
2021-02-06 04:42:36 +00:00
```
# --solutions--
```js
2021-10-31 23:08:44 -07:00
const myArray = [["John", 23], ["dog", 3]];
2021-02-06 04:42:36 +00:00
myArray.shift();
myArray.unshift(["Paul", 35]);
```