【问题标题】:Automate Access of Alpha Numerical index in json自动访问 json 中的 Alpha 数字索引
【发布时间】:2010-05-18 07:33:01
【问题描述】:

我在使用 jquery 访问 json 数据时有以下类型的索引

data.rows[0].student_1
data.rows[0].student_2
data.rows[0].student_3 and so on...

现在,我想像这样在循环中自动化这个东西

for(var i=1;i<length;i++)
{
  // so that i can access all student records i.e. student_1, student_2 and so on
 data.rows[0].student_+i; // this doesn't work

}

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

    使用数组样式属性访问器:

    data.rows[0]["student_"+i];
    

    【讨论】:

      【解决方案2】:

      你可以使用方括号:

      for(var i=1;i<length;i++)
      {
       data.rows[0]['student_'+i];
      }
      

      参见此处:http://24ways.org/2005/dont-be-eval

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-01
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        • 1970-01-01
        • 2017-04-09
        • 2010-09-05
        • 1970-01-01
        相关资源
        最近更新 更多