javascript findIndex callback arguments - Stack Overflow

I'm new to javascript.I'm trying to find the index of a specific element in an array. I read

I'm new to javascript.

I'm trying to find the index of a specific element in an array. I read about that I can use findIndex to loop through an array. But it seems that findIndex only accept three arguments: element, index and array. What if I want change the object that is used to be pared.

For example, I want for find the index of string 'b' in array ['a','b','c'],

var position = ['a','b','c'].findIndex(function(element, index, array){return element==='b'})

but how do I pass 'b' as parameters that I can change to callback function

Thanks

I'm new to javascript.

I'm trying to find the index of a specific element in an array. I read about that I can use findIndex to loop through an array. But it seems that findIndex only accept three arguments: element, index and array. What if I want change the object that is used to be pared.

For example, I want for find the index of string 'b' in array ['a','b','c'],

var position = ['a','b','c'].findIndex(function(element, index, array){return element==='b'})

but how do I pass 'b' as parameters that I can change to callback function

Thanks

Share Improve this question edited Jul 27, 2017 at 12:24 Talha Awan 4,6194 gold badges26 silver badges40 bronze badges asked Jul 27, 2017 at 11:19 X.ZX.Z 1,1482 gold badges11 silver badges16 bronze badges 1
  • 1 "findIndex only accept three arguments" not really. findIndex accepts 2 arguments: callback and optional context. It is a callback that is being called with 3 arguments. – Yury Tarabanko Commented Jul 27, 2017 at 11:27
Add a ment  | 

3 Answers 3

Reset to default 4

What about indexOf function? You just have to pass one argument, as searched element.

let arr = ['a','b','c'];

console.log(arr.indexOf('b'));

You can define the wanted character from the outside context inside the callback function:

var wantedChar = 'c';
var position = ['a','b','c'].findIndex(function(element, index, array){return element===wantedChar})

console.log(position);

By doing so, you can wrap all that up in a function:

var findPos = function(arr, char){
    return arr.findIndex(function(element, index, array){return element===char});
}
console.log(findPos(['a','b','c'], 'c'));

Note: as already suggested, it makes more sense to use indexOf when just paring strings. The findIndexfunction in bination with a custom callback is there for more sophisticated search, e.g. when dealing with plex structured objects.

function getPosition(){
var position = ["a","b","c"];
var a = position.indexOf("b");
document.getElementById("demo").innerHTML = a;
}
<button onclick="getPosition()">button</button>
<p id="demo"></p>

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

相关推荐

  • javascript findIndex callback arguments - Stack Overflow

    I'm new to javascript.I'm trying to find the index of a specific element in an array. I read

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信