freeCodeCamp/curriculum/challenges/portuguese/02-javascript-algorithms-an.../basic-javascript/accessing-nested-arrays.md

121 lines
1.9 KiB
Markdown
Raw Normal View History

---
id: 56533eb9ac21ba0edf2244cd
title: Acessar arrays aninhados
challengeType: 1
videoUrl: 'https://scrimba.com/c/cLeGDtZ'
forumTopicId: 16160
dashedName: accessing-nested-arrays
---
# --description--
2021-07-10 04:23:54 +00:00
Como vimos em exemplos anteriores, objetos podem conter tanto objetos aninhados quanto arrays aninhados. Semelhante ao acesso de objetos aninhados, a notação de colchetes pode ser encadeada para se acessar arrays aninhados.
2021-07-10 04:23:54 +00:00
Aqui está um exemplo de como se acessar um array aninhado:
```js
var ourPets = [
{
animalType: "cat",
names: [
"Meowzer",
"Fluffy",
"Kit-Cat"
]
},
{
animalType: "dog",
names: [
"Spot",
"Bowser",
"Frankie"
]
}
];
ourPets[0].names[1];
ourPets[1].names[0];
```
2021-07-10 04:23:54 +00:00
`ourPets[0].names[1]` seria a string `Fluffy` e `ourPets[1].names[0]` seria a string `Spot`.
# --instructions--
2021-07-10 04:23:54 +00:00
Usando a notação de ponto e de colchetes, defina a variável `secondTree` para o segundo item na lista de `trees` do objeto `myPlants`.
# --hints--
2021-07-10 04:23:54 +00:00
`secondTree` deve ser igual à string `pine`.
```js
assert(secondTree === 'pine');
```
2021-07-10 04:23:54 +00:00
Seu código deve usar notação de ponto e colchetes para acessar `myPlants`.
```js
assert(/=\s*myPlants\[1\].list\[1\]/.test(code));
```
# --seed--
## --after-user-code--
```js
(function(x) {
if(typeof x != 'undefined') {
return "secondTree = " + x;
}
return "secondTree is undefined";
})(secondTree);
```
## --seed-contents--
```js
var myPlants = [
{
type: "flowers",
list: [
"rose",
"tulip",
"dandelion"
]
},
{
type: "trees",
list: [
"fir",
"pine",
"birch"
]
}
];
var secondTree = "";
```
# --solutions--
```js
var myPlants = [
{
type: "flowers",
list: [
"rose",
"tulip",
"dandelion"
]
},
{
type: "trees",
list: [
"fir",
"pine",
"birch"
]
}
];
var secondTree = myPlants[1].list[1];
```