1

I want to get something like this: oranges

However no output is displayed. I tried printing via console and it doesn't work.

var array2 = ["Banana", ["Apples", ["Oranges"], "Blueberries"]];
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    can you give us your closest try please – jonatjano Aug 29 '18 at 13:35
  • 2
    array2[1][1][0]; – Joshua Ibemgbo Aug 29 '18 at 13:37
  • 3
    @JoshuaIbemgbo That's what you need to use, and it works... so what's the problem? – Rory McCrossan Aug 29 '18 at 13:38
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/q/11922383/608639), [How to access a element in JavaScript array?](https://stackoverflow.com/q/15995780/608639), [JavaScript access array elements by object value](https://stackoverflow.com/q/30194237/608639), [How to get value at a specific index of array In JavaScript?](https://stackoverflow.com/q/8238456/608639), etc. – jww Aug 29 '18 at 13:57

4 Answers4

3

Since the desired fruit resides inside into third level, you have to use 3 indexes. Try array2[1][1][0].

Step by step:

array2[1]        => ["Apples", ["Oranges"], "Blueberries"]
array2[1][1]     => ["Oranges"]
array2[1][1][0]  => Oranges

var array2 = ["Banana", ["Apples", ["Oranges"], "Blueberries"]];

console.log(array2[1][1][0]); // Oranges
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • 1
    Shouldn't questions like this be closed against their canonical dups? What value does this questions and the answers add to the site for future visitors? – jww Aug 29 '18 at 14:03
2

you can use Destructuring assignment on your array

but that's seem like overkill here

var array2 = ["Banana", ["Apples", ["Oranges"], "Blueberries"]];

let [,[,[orange]]] = array2

console.log(orange)

I added this answer to inform this way existed, BUT the answer from @Mamum is the way to go to when you need to get a small number of values from an array :

let orange = array2[1][1][0]
jonatjano
  • 3,576
  • 1
  • 15
  • 20
1

Welcome Joshua!

Arrays are accessible starting from index 0, so in order to access the inside array ["Apples", ["Oranges"], "Blueberries"] you need to write array2[1] since it is the second item in the array.

Next you need to access the ["Oranges"] which is also the second item in that array, resulting in array2[1][1].

Finally since array2[1][1] also returns an array containing "Oranges" in its first index (0), you have to write array2[1][1][0] which will give you the result

console.log(array2[1][1][0])
Bamieh
  • 10,358
  • 4
  • 31
  • 52
0

Your arrays are currently nested, are you sure that's what you want? Since you have a pretty linear list of items here, I would suggest taking the internal arrays out:

var array2 = ["Banana", "Apples", "Oranges", "Blueberries"];

Array contents can be called by using the index number associated with the item you are trying to select. In this case:

console.log(array2[2]);

Here is the MDN documentation on JavaScript arrays, for your reference :)

Jensen010
  • 445
  • 6
  • 18