Is there a good way to check if all indexes in an array are strings?
check(["asd", 123]); // false
check(["asd", "dsa", "qwe"]); // true
Is there a good way to check if all indexes in an array are strings?
check(["asd", 123]); // false
check(["asd", "dsa", "qwe"]); // true
You can use Array.every to check if all elements are strings.
const isStringsArray = arr => arr.every(i => typeof i === "string")
console.log(
isStringsArray(['a','b','c']),
isStringsArray(['a','','c']),
isStringsArray(['a', ,'c']),
isStringsArray(['a', undefined,'c']),
isStringsArray(['a','b',1])
)
_.every(x, _.isString);function allElementsAreString(arr => !arr.some(element => typeof element !== "string")) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…some and every are "short circuiting", meaning that they will stop as soon as the condition is not satisfied for every or as soon as it is satisfied for some.Something like this?
var data = ["asd", 123];
function isAllString(data) {
var stringCount;
for (var i = 0; i <= data.length; i++) {
if (typeof data[i] === 'string')
stringCount++;
}
return (stringCount == data.length);
}
if (isAllString(data)) {
alert('all string');
} else {
alert('check failed');
}