0

i'm trying to access to an array object, but i just receive a "undefined" message on my console, i tried several ways of this forum, but no one helped me, please show me how to access itenter image description here

4
  • Like, var arrObjs = [{'administrator_id': 3}, {'administrator_id': 4}]; var firstAdministratorId = arrObjs[0].administrator_id Commented Aug 27, 2016 at 4:43
  • 2
    The screenshots shows that the array is empty when you log it. It smells like an asynchronicity problem. Commented Aug 27, 2016 at 5:19
  • Please show your code Commented Aug 27, 2016 at 7:23
  • 1
    There is about a 90% chance you logged the array to the console at a point in time when it was not yet populated (for example, immediately after invoking an asynchronous call, but before it finished). Then, when you opened up the array in the console by pressing on the little black arrow, the data had now been retrieved, so it is displayed. Make sure you don't try to access the array before the asynchronous call finished. In other words, access it only in the then or subscribe handler or callback. Commented May 9, 2017 at 7:41

4 Answers 4

3

It is no clear which array you want to access. In javascript it is simple to access array with Index like nameOfarray[Index].property_name.

var user=[
  {'name':'abc',id:1,'subject':[{'math':90,'english':80}]},
  {'name':'abc1',id:2,'subject':[{'math':90,'english':80}]},
  {'name':'abc2',id:3,'subject':[{'math':90,'english':80}]}
];

val name=user[0].name; var math_mark=user[0].subject[0].math;

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

Comments

0

What you have here is basically an array of objects.

Let's say arrayobj represents your variable that contains this whole thing that you've done a console.log of, so to access, say, picturepath of the first object (which has index 0 in the array), you can write -

arrayobj[0].picturepath

Similarly, by changing the index (of the array), you can get to the next object and to access object elements, you have to use .elementindex

So in general -

array[arrayindex].objectindex should give you the required results.

Comments

-1

If you want to do operations on an object that is printed to the chrome console, right click on the object and do Store as global variable. Then you can access it using the name temp1 or whatever is printed out in the console.

Comments

-1

You can access array values by entering index of theme.

for example array[0].administrators_id return 3

if you love firefox , you can use firefox. in Firefox console by click on array you can see it's content on right side.

Firefox Console Show Array

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.