0

I have a 2d array containing data like the following:

[ 
  [ 'name', 'mike' ],
  [ 'type', 'person' ],
  [ 'age', '24' ]
]

I need to access the value of the second element of the inner array, based on the value of the first element in the inner array.

So basically I want to specify "name" and in return, receive the value "mike". How can I accomplish this without looping through the entire array?

3
  • You can't do it without looping through the array (although you can stop the loop when you find the correct value). If at all possible, you should consider using objects instead. Commented Jun 1, 2014 at 13:31
  • 2
    why don't you use an object? { name: "mike", type: "person", age: 24 } Commented Jun 1, 2014 at 13:31
  • 2
    This is what objects are for. Commented Jun 1, 2014 at 13:31

1 Answer 1

1

As Juhana said, use objects:

obj = { 
  name: 'mike',
  type: 'person',
  age: 24
};

Then you can access the value of name by obj.name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.