2021-06-15 03:34:20 +09:00
---
id: 56bbb991ad1ed5201cd392cc
title: Manipolare gli array con pop()
challengeType: 1
videoUrl: 'https://scrimba.com/c/cRbVZAB'
forumTopicId: 18236
dashedName: manipulate-arrays-with-pop
---
# --description--
Un altro modo per cambiare i dati in un array è con la funzione `.pop()` .
`.pop()` è usato per estrarre un valore dalla fine di un array. Possiamo memorizzare questo valore assegnandolo ad una variabile. In altre parole, `.pop()` rimuove l'ultimo elemento da un array e restituisce quell'elemento.
Qualsiasi tipo di elemento può essere estratto da un array - numeri, stringhe, anche array annidati.
```js
2021-10-27 15:10:57 +00:00
const threeArr = [1, 4, 6];
const oneDown = threeArr.pop();
2021-06-15 03:34:20 +09:00
console.log(oneDown);
console.log(threeArr);
```
Il primo `console.log` mostrerà il valore `6` e il secondo mostrerà il valore `[1, 4]` .
# --instructions--
2021-10-27 15:10:57 +00:00
Utilizza la funzione `.pop()` per rimuovere l'ultimo elemento da `myArray` e assegna il valore estratto a `removedFromMyArray` .
2021-06-15 03:34:20 +09:00
# --hints--
`myArray` dovrebbe contenere solo `[["John", 23]]` .
```js
assert(
(function (d) {
if (d[0][0] == 'John' & & d[0][1] === 23 & & d[1] == undefined) {
return true;
} else {
return false;
}
})(myArray)
);
```
Dovresti usare `pop()` su `myArray` .
```js
assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
```
`removedFromMyArray` dovrebbe contenere solo `["cat", 2]` .
```js
assert(
(function (d) {
if (d[0] == 'cat' & & d[1] === 2 & & d[2] == undefined) {
return true;
} else {
return false;
}
})(removedFromMyArray)
);
```
# --seed--
## --after-user-code--
```js
2021-10-27 15:10:57 +00:00
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
2021-06-15 03:34:20 +09:00
```
## --seed-contents--
```js
// Setup
2021-10-27 15:10:57 +00:00
const myArray = [["John", 23], ["cat", 2]];
2021-06-15 03:34:20 +09:00
// Only change code below this line
2021-10-27 15:10:57 +00:00
2021-06-15 03:34:20 +09:00
```
# --solutions--
```js
2021-10-27 15:10:57 +00:00
const myArray = [["John", 23], ["cat", 2]];
const removedFromMyArray = myArray.pop();
2021-06-15 03:34:20 +09:00
```