How to find out if there is an element in the array, but without regard to the data type, i.e. if the array has [(int)1] , then the search (string)"1" returns true ?
|
2 answers
var test = [1, 2, 3]; function hasValue(arr, value) { return arr.findIndex(item => item == value) != -1; } console.log(hasValue(test, "2")? "found" : "not found"); - It would be more logical to use
arr.some- Alexey Ten
|
If I understood everything correctly, then the Array.includes (item) method works exactly as you need, the only question is what types you can have there, if only strings and numbers, then:
var mixedArray = [1,'2',3] function hasElement(arr, element) { return arr.includes(String(element)) // или Number(element), но лучше приводить к строке } hasElement(mixedArray, 2) //true hasElement(mixedArray, '2') //true |