从postman / newman中的值获取JSON对象的索引

时间:2017-11-22 11:11:24

标签: javascript postman newman

我试图通过传递值的引用来检索索引。

我有一些javascript选项,但所有这些功能都显示is not a function

获得如下错误:

TypeError | jsonData.getIndexOf is not a function

我尝试过以下链接中提到的选项:

get index from a JSON object with value

示例:

var jsonData = JSON.parse(responseBody);

Array.prototype.getIndexOf = function(el) {

  var arr = this;

  for (var i=0; i<arr.length; i++){
     console.log(arr[i].name);
     if(arr[i].name==el){
       return i;
     }

  }

  return -1;

}

console.log(jsonData.getIndexOf("Volume"));

对象截图:

enter image description here

邮递员/纽曼是否有可能

1 个答案:

答案 0 :(得分:0)

我使用此更新代码取得了成功:

var jsonData = JSON.parse(responseBody);

//I would use an Object prototype here, because jsonData is an object
Object.prototype.getIndexOf = function(el) {
  var arr = this;
  
  for (var i=0; i<arr.length; i++){
      //when you're checking to see if a key exists, it is better to use the "in" operator
     if(el in arr[i]){
       return i;
     }
  }

  return -1;
}

//make sure the case matches
console.log(jsonData.getIndexOf("Volume"));