-5

I'm wondering how to get the findindex of a specific service_name in the below array?

var obj = {"ID":111222,"NAME":"Chicken","FREQUENCY":"Yearly","service_name":["Open ticket","Service Account time","Assets Management Overview"]}

I have tried the following but not getting the correct index

var find_index = obj.service_name.findIndex(function(obj){return obj.service_name = "Open ticket"})
console.log ("The index of " + find_index);
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
user3292394
  • 609
  • 2
  • 11
  • 24

1 Answers1

-1

Your code is close, but there are a couple of semantic issues. You're already iterating over the service_name array so the argument that the .findIndex callback takes is the array element / string itself. You also need to use == or === for comparison. Right now you are performing assignment.

const find_index = obj.service_name.findIndex(
  serviceName => "Open ticket" === serviceName
);

In this case you can also use .indexOf which would be a bit simpler:

const index = obj.service_name.indexOf("Open ticket");
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405