1

I have an array like this

let a = ["1","2","3"];
let searchFor = 1;
_includes(a, searchFor); //this returns false

I think this one returns false because the value in array a are strings instead of number.

How can I disregard the data type?

I don't want to convert those strings in the array into numbers because some values in there might be strings really.

Thanks.

PinoyStackOverflower
  • 5,214
  • 18
  • 63
  • 126

1 Answers1

2

You can create your own loose includes with Array.some() (or lodash's _.some()), and an abstract equality comparison (==):

const loostIncludes = (arr, value) => arr.some(v => v == value)
  
console.log(loostIncludes(["1","2","3"], 1))
console.log(loostIncludes(["1","2","3"], "1"))
console.log(loostIncludes(["1","2","3"], 4))
Ori Drori
  • 183,571
  • 29
  • 224
  • 209