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

时间:2021-11-13 23:46:55

Under rows, I want to check and see if I have a value of Submit and the integer value that comes after it. How can I retrieve these two values without using Array.filter()? Google Analytics JS api doesn't like the filter command.

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

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

3 个解决方案

#1


1  

Just use a for loop...

只需使用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

#2


0  

Assuming the structure you have in your question is assigned to returnData:

假设您的问题中的结构被分配给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
}

#3


0  

Here is an alternative, if you don't wanna use a loop try this :

这是另一种选择,如果你不想使用循环试试这个:

// 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);

Best regards

#1


1  

Just use a for loop...

只需使用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

#2


0  

Assuming the structure you have in your question is assigned to returnData:

假设您的问题中的结构被分配给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
}

#3


0  

Here is an alternative, if you don't wanna use a loop try this :

这是另一种选择,如果你不想使用循环试试这个:

// 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);

Best regards