4

I have a JS array:

a = ["a",["b","c"]]

How I can access string "b" in this array? Thank you very much!

asked Apr 14, 2013 at 4:23
0

3 Answers 3

7

You index into an array like this:

a[1][0]

Arrays are accessed using their integer indexes. Since this is an array inside an array, you use [1] to access the inner array, then get the first item in that array with [0].

answered Apr 14, 2013 at 4:24
Sign up to request clarification or add additional context in comments.

Comments

4

That is a[1][0]

alert(a[1][0]) // "b"
Derek 朕會功夫
94.8k45 gold badges199 silver badges255 bronze badges
answered Apr 14, 2013 at 4:25

Comments

2

As there is an alternative way also to access the element in the array which is:

a['1']['0'] //"b"

as array is internally an object so think indexes is a property of that object so

a = ["a",["b","c"]]

can be internally and object keys or property are internally transfer to string so:

a = {
 '0' : "a",
 '1' : ["b", "c"]
}

this also can refactor to:

a = {
 '0' : "a",
 '1' : {
 '0' : "b",
 '1' : "c"
 }
}

so we can access that index as:

a['1']['0']

this will give the value as b.

Peyman Mohamadpour
18.1k24 gold badges94 silver badges102 bronze badges
answered May 27, 2020 at 5:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.