简体   繁体   English

Javascript:如何访问数组对象?

[英]Javascript: How to access an array object?

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 it 我正在尝试访问数组对象,但是我只是在控制台上收到一条“未定义”消息,我尝试了该论坛的几种方法,但是没有人帮助我,请向我展示如何访问它 在此处输入图片说明

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 . 在javascript中,使用nameOfarray[Index].property_name类的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; val name = user [0] .name; var math_mark=user[0].subject[0].math; var math_mark = user [0] .subject [0] .math;

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代表了你的变量包含您所做的的console.log,所以要获得这整个事情,比方说, picturepath第一目标(其序号为0阵列中),你可以写-

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 同样,通过更改(数组的)索引,您可以转到下一个对象并访问对象元素,必须使用.elementindex

So in general - 所以总的来说-

array[arrayindex].objectindex should give you the required results. array[arrayindex].objectindex应该会为您提供所需的结果。

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 . 如果要对打印到chrome控制台的对象执行操作,请右键单击该对象,然后执行“ Store as global variable Then you can access it using the name temp1 or whatever is printed out in the console. 然后,您可以使用名称temp1或控制台中打印出的任何名称来访问它。

You can access array values by entering index of theme. 您可以通过输入主题索引来访问数组值。

for example array[0].administrators_id return 3 例如array[0].administrators_id返回3

if you love firefox , you can use firefox. 如果您喜欢Firefox,可以使用Firefox。 in Firefox console by click on array you can see it's content on right side. 在Firefox控制台中,通过单击数组,您可以在右侧看到其内容。

Firefox控制台显示数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM