0

I have an array with 10 elements. Each element is also an array and each of their children are objects.

Lets call it "array".

array[0] gives me the first element. It is filled with 20 objects.

I want to get the first one, and then a value in that object, such as this:

var id = array[0].getFirstElement().id;

How do I do it?

I tried array[0][0] and other things but didn't work.

4
  • 1
    Can you post your code that creates this array? Commented Jul 18, 2016 at 22:47
  • 2
    arr[0][0].id should work in javascript Commented Jul 18, 2016 at 22:48
  • post a data sample Commented Jul 18, 2016 at 22:53
  • Sorry, array[0][0].id works, I was careless. Not deleting question, maybe someone will need. Commented Jul 18, 2016 at 22:53

2 Answers 2

3

For this example

array = [[{id:2, ...}, ...], ...]

You need array[0][0].id

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

Comments

0

See a for in loop: here

for you something like this may work (not tested)

for(var prop in array[0]){
  if( array[0].hasOwnProperty( prop ) ) {
    console.log("array[0]." + prop.id + " = " + array[0][prop].id);
  } 
  //and because you only want the first
  break;
}

2 Comments

array[0] is an array itself, so a for-in loop is not ideal there.
Gave this as an answer because he said it was filled with objects... Seems like I misunderstood a little. Thought he was looking for array[0].object1.id, where the name object1 would be unknown... This would work in that case ;)

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.