-5

I really need your help,

I would like to be able to check and see if a variable matches an array value and return true if it does.

ie.

var x = "ASFA"

var array = ["OTHER-REQUEST-ASFA", "OTHER-REQUEST-ASFB", "OTHER-REQUEST-ASFC"]

alert("true")

I was thinking of using this approach, but for the life of me, I cannot get it to return true, ideas?

function test() {

    var arr = ["OTHER-REQUEST-ASFA","OTHER-REQUEST-ASFB","OTHER-REQUEST-ASFC"]

    if ( $.inArray('ASFA', arr) > -1 ) {

        alert("true")

    }

}
BobbyJones
  • 1,331
  • 1
  • 26
  • 44

2 Answers2

0

Try as follows

var x = "ASFA"

var array = ["OTHER-REQUEST-ASFA", "OTHER-REQUEST-ASFB", "OTHER-REQUEST-ASFC"]


array.forEach(function(ele){

console.log(ele.includes(x));




})
Nisal Edu
  • 7,237
  • 4
  • 28
  • 34
0

Quick and easy with ES6 :

let x = "ASFA",
    array = ["OTHER-REQUEST-ASFA", "OTHER-REQUEST-ASFB", "OTHER-REQUEST-ASFC"],
    found = array.some(elem => elem.includes(x))
    
console.log(found)
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63