L
L
limpopo19922015-04-12 17:26:03
JavaScript
limpopo1992, 2015-04-12 17:26:03

How to access the elements of a JavaScript array?

Here in this example:

var user = '{ "name": "Вася", "age": 35, "isAdmin": false, "friends": [0,1,2,3] }';

user = JSON.parse(user);

alert( user.friends[1] ); // 1

we turn to friends and select its second element, and if I have
[{ "thelocation" : "Moscow", "":""},{ "thelocation" : "london", "":""}], then how should I turn to the second thelocation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Petrov, 2015-04-12
@limpopo1992

user.friends[1].thelocation
Do you read manuals at all? Well, at least what textbook ? Or a book ?
Update: I never cease to be amazed...

var users = [{ "thelocation" : "москва", "":""},{ "thelocation" : "london", "":""}];
console.log(users[1].thelocation);

P
Pretor DH, 2015-04-12
@PretorDH

var user = { "name": "Вася", "age": 35, "isAdmin": false, "friends": [0,1,2,3] };
var friends = [{ "thelocation" : "москва", "":""},{ "thelocation" : "london", "":""}];
    friends[0].thelocation //moskwa
    friends[1]['thelocation'] //london

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question