Finding an item of an array of numbers without using a loop

前端 未结 5 812
借酒劲吻你
借酒劲吻你 2021-01-03 13:05

This is a stupid question, it feels like one. But mental block is bad right now. :(

My problem is I have an array consisting only of numbers. I want to use that arra

5条回答
  •  佛祖请我去吃肉
    2021-01-03 13:21

    A simple loop seems called for here, but-

    You can do it without a loop, and without extending IE, if you convert the array to a string.

    var a = [2,4,6,8,10], n = 2;

    if(RegExp('\\b'+n+'\\b').test(a.join(','))){
    // n is in a
    }
    

提交回复
热议问题