如何在不使用Array.filter()的情况下从JSON对象获取特定值?

时间:2014-12-14 18:23:16

标签: javascript json

rows下,我想查看是否有值Submit以及后面的整数值。如何在不使用Array.filter()的情况下检索这两个值? Google Analytics JS api不喜欢过滤器命令。

{
  "rows": [
        [
      "Load",
      "11"
    ],
    [
      "No Thanks",
      "7"
    ],
    [
      "NoThanks",
      "3"
    ],
    [
      "No_Thanks",
      "2"
    ],
    [
      "Results",
      "88"
    ],
    [
      "Sent",
      "1"
    ],
    [
      "Sign Up",
      "9"
    ],
    [
      "XOut",
      "161"
    ],
    [
      "Submit",
      "30"
    ]
  ]
}

3 个答案:

答案 0 :(得分:1)

只需使用for循环......

var submitFound
for(var i = 0; i < rows.length; i++) {
  var row = rows[i]
  if (row[0] === 'Submit') {
    submitFound = true
    break
  }
}
if (submitFound) // do stuff

答案 1 :(得分:0)

假设您的问题中的结构已分配给returnData:

var returnedData = {...}; //Structure from the question
var value = -1;
for(var i = 0; i < returnedData.rows.length; i++){
    var row = returnedData.rows[i];
    if (row[0] == 'Submit') {
        value = row[1];
        break;
    }
}

if (value > -1) {
    // Do whatever with the value
}

答案 2 :(得分:0)

如果您不想使用循环,可以选择以下方法:

// this object is simplified for test

var json={
  "rows": [
        [
      "Load",
      "11"
    ],
    [
      "Submit",
      "30"
    ]
  ]
};

var stringJson=JSON.stringify(json);
var valueOfSubmit= stringJson.match("\"Submit\"\,\"([^)]+)\"\]")[1];
alert(valueOfSubmit);

祝你好运