I have a csv value and need to check that one of the csv values in my array units available.
Why do I get in both cases true?
let arr = [16]
let check = _.includes('14,15,16,17,18,19', arr)
console.log(check);
arr = [6]
check = _.includes('14,15,16,17,18,19', arr)
console.log(check);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
The signature of _includes
is
_.includes(collection, value, [fromIndex=0])
And in case the collection
is a string - which it is here:
If collection is a string, it's checked for a substring of value
And both values you're passing - [16]
and [6]
- when coerced to strings - exist as substrings. ('16'
and '6'
)
It sounds like you might want to turn the string into an array of numbers first - and check against only a single value, not an array. Eg:
const inputString = '14,15,16,17,18,19';
const arrOfNumbers = JSON.parse(`[${inputString}]`);
console.log(arrOfNumbers.includes(16));
console.log(arrOfNumbers.includes(6));