Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

get index of Javascript object with Array

douglas_rogers2
Kilo Contributor

What should be the ideal solution for this

Input: var inc2 = {
  num
: 'INC111',
  sd
: 'email'
};
var inc2 = {
  num
: 'INC222',
  foo
: 'internet'
}

var arr = [];
arr.push(inc2,inc1);

Index of the inc1 object to be 1.

1 ACCEPTED SOLUTION

antin_s
ServiceNow Employee
ServiceNow Employee

From the title, i see you are looking for getting the index of an array in Javascript object( based on a value)? Please correct me if I am wrong.



If thats the case, you can use 'map' function. Also, I think the inc2 one line # 1 is typo, I corrected it.



var inc1 = {


  num: 'INC111',


  sd: 'email'


};


var inc2 = {


  num: 'INC222',


  foo: 'internet'


}




var arr = [];


arr.push(inc2,inc1);




pos = arr.map(function(input) { return input.num; }).indexOf('INC111');




gs.log(pos);



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


View solution in original post

7 REPLIES 7

Sure!


Thanks. This should do though I wanted it little differently.


Glad it helped. But can you explain what you wanted differently?



Thanks


Antin