admin管理员组文章数量:1332865
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
2 Answers
Reset to default 3findIndex
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.
本文标签: arraysJavaScript findIndex not working for first objectStack Overflow
版权声明:本文标题:arrays - JavaScript findIndex not working for first object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292607a2448089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论