My snippet looks something like this:
let go = "hello";
let col =["12","hello","14","15"];
let f = _.some(col,go);
I want to check whether the go value is present in col TRUE/FALSE. When I run this I get , f=false? How can I fix this or how to check the presence of a string in an array of strings (with lodash)?
You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.
isString() Method. The _. isString() method is used to find whether the given value is a string object or not. It returns True if the given value is a string.
The Lodash _. isArray() method checks if the given value can be classified as an Array Value or not.
Check if a String is contained in an Array using indexOf # We used the Array. indexOf method to check if the string two is contained in the array. If the string is not contained in the array, the indexOf method returns -1 , otherwise it returns the index of the first occurrence of the string in the array.
Should work
let go = "hello";
let col =["12","hello","14","15"];
let f = _.includes(col,go);
https://lodash.com/docs#includes
With only javascript you can use indexOf
. If it is present it will return the index of the element else -1
let go = "12";
let col = ["12", "hello", "14", "15"];
var isElemPresent = (col.indexOf(go) !== -1) ? true : false
console.log(isElemPresent)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With