-1

I am trying to retrieve an index by passing a reference of a value.

I have some options of javascript but all those functions shows is not a function

Getting error like below:

TypeError | jsonData.getIndexOf is not a function

I have tried options mentioned in below link:

get index from a JSON object with value

Example:

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

Object screenshot:

enter image description here

Is it possible in postman/newman

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • can you post some of your data from `responseBody` that you work on? – Kresimir Pendic Nov 22 '17 at 11:14
  • What is the output of `console.log(jsonData instanceof Array)`? – Quentin Nov 22 '17 at 11:15
  • I can't post exactly my response as per the rules. But that JSON is huge with so many key and nested arrays etc. – Shubham Jain Nov 22 '17 at 11:15
  • JSON objects are most likely to have alphanumerical keys and be converted to objects intead of arrays. You might need to extend object prototype too – Kaddath Nov 22 '17 at 11:15
  • @ShubhamJain — "But that JSON is huge" — this why you need to construct a [mcve] – Quentin Nov 22 '17 at 11:16
  • @Kaddath — The JSON text in the linked question has an array as the outermost data type. – Quentin Nov 22 '17 at 11:16
  • @Quentin - I have created a dummy data and posted on this URL: https://gist.github.com/anonymous/0f8fe79452b254514beaeb229ba67a6b – Shubham Jain Nov 22 '17 at 11:20
  • Now suppose I require the index from value as "Sharpe Oneal" .. Any solution will be helpful – Shubham Jain Nov 22 '17 at 11:21
  • @ShubhamJain — Don't just create dummy data. You need to create a [mcve]. One that actually demonstrates the problem. Your code doesn't throw any error with that data: https://jsfiddle.net/hxLj7c83/ – Quentin Nov 22 '17 at 11:24
  • actually your code works perfectly here with a sample of the external link you posted (3 first rows). That's why the JSON should be here, we should be able to test that it actually doesn't work with what you provided, to see if the error does not come from something else – Kaddath Nov 22 '17 at 11:24
  • Yes, I have seen jsfiddle url and it's returning me -1 with value present in the JSON(it's still wrong) but yes it is not throwing error as I am getting in postman Tests section. looks intresting. – Shubham Jain Nov 22 '17 at 11:28
  • But i still consider that you should extend Object instead of Array, an Array being an Object, it will work in all cases (and use `for...in` because an object has no length) – Kaddath Nov 22 '17 at 11:30
  • @Kaddath — The function expects the object it is called on to have a `length` property (which an array will have). It doesn't make much sense to stick it on every kind of object. – Quentin Nov 22 '17 at 11:31
  • @Kaddath - it's not working .. I have added a screen shot about same – Shubham Jain Nov 22 '17 at 11:32
  • @ShubhamJain if you get the same error while the var is obviously also an object, it may come from your framework. I don't know postman, are you sure it lets you extend prototypes? – Kaddath Nov 22 '17 at 11:36
  • Yes maybe as postman is not allowing even another module to get into it.. hmm .. BTW thanks you all for trying – Shubham Jain Nov 22 '17 at 11:41

1 Answers1

0

I had success using this updated code:

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"));
Ed Meacham
  • 543
  • 1
  • 5
  • 19