arrays - JavaScript findIndex not working for first object - Stack Overflow

var data=[{"vpnkey":"CUSTOMER_NAME","validation":"ALPHANUMERIC"

var data=[{"vpnkey":"CUSTOMER_NAME","validation":"ALPHANUMERIC"},
{"vpnkey":"VRF","validation":"VRF_CHECK"},
{"vpnkey":"MOBILE_ADDRESS_SUMMARIZED_RANGE","validation":"IP_MASK"},
{"vpnkey":"APN_MOBILE_RANGE","validation":"IP_MASK"},
{"vpnkey":"FIXED_IP_LOOPBACK_TRACK_ID","validation":"NUMERIC"},
{"vpnkey":"CUSTOMER_BGP_REMOTE_AS","validation":"NUMERIC"},
{"vpnkey":"testing_purpass1","validation":"IP_ADDRESS"},
{"vpnkey":"testing_purpass2","validation":"IP_ADDRESS"}]

when i search vpnkey With first object "vpnkey value" it is returning -1 but expected result 0

this.vpnKeys["Commonkeys"].findIndex(function(item, i){
   if(item.vpnkey === "CUSTOMER_NAME") return i
});

when i search based on vpnkey other then first object "vpnkey value" it gives proper index value

this.vpnKeys["Commonkeys"].findIndex(function(item, i){
   if(item.vpnkey === "VRF") return i
});
var data=[{"vpnkey":"CUSTOMER_NAME","validation":"ALPHANUMERIC"},
{"vpnkey":"VRF","validation":"VRF_CHECK"},
{"vpnkey":"MOBILE_ADDRESS_SUMMARIZED_RANGE","validation":"IP_MASK"},
{"vpnkey":"APN_MOBILE_RANGE","validation":"IP_MASK"},
{"vpnkey":"FIXED_IP_LOOPBACK_TRACK_ID","validation":"NUMERIC"},
{"vpnkey":"CUSTOMER_BGP_REMOTE_AS","validation":"NUMERIC"},
{"vpnkey":"testing_purpass1","validation":"IP_ADDRESS"},
{"vpnkey":"testing_purpass2","validation":"IP_ADDRESS"}]

when i search vpnkey With first object "vpnkey value" it is returning -1 but expected result 0

this.vpnKeys["Commonkeys"].findIndex(function(item, i){
   if(item.vpnkey === "CUSTOMER_NAME") return i
});

when i search based on vpnkey other then first object "vpnkey value" it gives proper index value

this.vpnKeys["Commonkeys"].findIndex(function(item, i){
   if(item.vpnkey === "VRF") return i
});
Share edited Nov 1, 2019 at 17:41 Scott Marcus 65.9k6 gold badges53 silver badges80 bronze badges asked Nov 1, 2019 at 17:38 Vinay ursVinay urs 731 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

findIndex will return the index of the first element in the array that satisfies the condition. So instead of returning i you can return item.vpnkey === "CUSTOMER_NAME"

var data = [{
  "vpnkey": "CUSTOMER_NAME",
  "validation": "ALPHANUMERIC"
}, {
  "vpnkey": "VRF",
  "validation": "VRF_CHECK"
}, {
  "vpnkey": "MOBILE_ADDRESS_SUMMARIZED_RANGE",
  "validation": "IP_MASK"
}, {
  "vpnkey": "APN_MOBILE_RANGE",
  "validation": "IP_MASK"
}, {
  "vpnkey": "FIXED_IP_LOOPBACK_TRACK_ID",
  "validation": "NUMERIC"
}, {
  "vpnkey": "CUSTOMER_BGP_REMOTE_AS",
  "validation": "NUMERIC"
}, {
  "vpnkey": "testing_purpass1",
  "validation": "IP_ADDRESS"
}, {
  "vpnkey": "testing_purpass2",
  "validation": "IP_ADDRESS"
}]



const x = data.findIndex(function(item, i) {
  return item.vpnkey === "CUSTOMER_NAME"
});
console.log(x)

findByIndex method returns the index that satisfies the provided testing function (the condition) in a given array, that's the whole purpose of that method.

The key is understand the value that returns the function, which is the index of where the condition is fulfilled, here is a small example:

var friendPets = ['Dog', 'Cat', 'Turtle'];

var someFriend = friendPets.findIndex(function (pet) {
  return pet === 'Turtle';
});

var otherFriend = friendPets.findIndex(function (pet) {
  return pet === 'Trex';
});

console.log(someFriend); //2 since one friend has a turtle in the position 2 of the array
console.log(otherFriend); // -1 since Trex is extinct (is not found in the array)

So think the condition in terms of a Boolean evaluation, can be either true or false. When it es to be true, then it founds and item with the given condition, if not (false) then returns -1.

You may ask you an important question, so why are the other options given in the callback function. Well those are parameters for additional information, specifically the one you were using, the i provides the index of the current element being processed in the array.

You may find all the details about this findIndex method in MDN's docs. It is worthy to take a look to these docs since many of the JavaScript Standard Built-in methods behave similar.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742292607a4416481.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信