如何在javascript数组中访问responsedata包含对象

时间:2015-01-21 07:47:48

标签: javascript jquery arrays

在javascript数组中,包含值(即; type是object)。该对象包含另一个值数组。在这个数组里面有3个对象。而对象有一些变数。如何在javascript中将一个变量推送到另一个数组

var studentData = new Array();

success: function(responseData) {
    $.each(responseData, function(index, item) { 
        studentData.push(responseData.markMasterList(0).markMasterDetails(i).student.name);

responseData显示..

Array[5]
>0: Object 
   >markArray: Array[1]
      >0: Object
        >markDetailsArray: Array[10]
           >0: Object
               >student: Object
                  >name  //How to push this variable to studentData
           >1: Object
           >2: Object
           >3: Object
>1: Object

我试过这个,但在控制台上出错了

studentData.push(responseData.markMasterList(0).markMasterDetails(i).student.name);

错误显示:

  

未捕获的TypeError:对象不是函数

1 个答案:

答案 0 :(得分:0)

如果没有更多的片段,我只能猜到,但你可能错过了数组语法。

您需要 [] 来引用数组(/ object)键,而不是()

studentData.push(responseData.markMasterList[0].markMasterDetails[i].student.name);

(我知道,你的问题是指对象,但在JS中,数组和对象有很多共同点。例如:)

var a = { 1 : "a" , 2 : "b" };
a[ 1 ] // Gives "a"