[英]how to get false if array have string value of index in javascript
function SummPositive( numbers ) {
var negatives = [];
var sum = 0;
for(var i = 0; i < numbers.length; i++) {
if(numbers[i] < 0) {
negatives.push(numbers[i]);
}else{
sum += numbers[i];
}
}
console.log(negatives);
return sum;
}
var sum_result = SummPositive( [ 1, 2, 3, 4, 5, -2, 23, -1, -13, 10,-52 ] );
console.log(sum_result);
i have some trouble, i dont know how to check if array have index string, return false, thx all for help me to fix it我有一些麻烦,我不知道如何检查数组是否有索引字符串,返回 false,谢谢大家帮助我修复它
function SummPositive( numbers ) {
let negatives = []; // hold all negative number
let falsy = false; // boolean to hold true if string exists in the list
let sum = 0; // to increment all the numbers in the list
for(let num of numbers) { // loop through the list
if(typeof num == 'string') {
falsy = true // set boolean to true if string item exists in the list
break // break out of the loop
}
if (num < 0) {
negatives.push(num); // push all negative numbers to a negatives list
} else {
sum += num // update sum
}
}
return falsy ? false : sum; // return false if falsy is true else return sum
}
var sum_result = SummPositive([ '44', 1, 2, 3, 4, 5, -2, 23, -1, -13, 10,-52, '3' ]);
The direct answer to your question of "How to check if an array element is a string" is to use the typeof
operator , specifically comparing the evaluated result to string
:您的“如何检查数组元素是否为字符串”问题的直接答案是使用typeof
运算符,特别是将评估结果与string
进行比较:
// const element = array[index];
const isAString = typeof element === 'string';
Here's an example showing how to use various array methods to help with the details of your problem.这是一个示例,展示了如何使用各种数组方法来帮助解决问题的细节。
function sum (numbers) { let sum = 0; for (const n of numbers) sum += n; return sum; } function sortAndStringify (numbers) { // Create a copy so the original object is not mutated by sorting const copy = [...numbers]; return copy.sort((a, b) => a - b).join(', '); } function example (array) { const stringWasFound = array.some(element => typeof element === 'string'); console.log('string was found:', stringWasFound); // Even better (more strict): ensure that all elements are numbers const allElementsAreNumbers = array.every(element => typeof element === 'number'); console.log('all elements are numbers:', allElementsAreNumbers); // Return false if a non-number was found if (!allElementsAreNumbers) return false; // If we get this far, then we know that we are only dealing with numbers const negatives = array.filter(n => n < 0); console.log('negatives:', sortAndStringify(negatives)); const positives = array.filter(n => n >= 0); console.log('positives:', sortAndStringify(positives)); console.log('sum:', sum(positives)); } console.log('Just numbers:'); example([1, 2, 3, 4, 5, -2, 23, -1, -13, 10, -52]); console.log('With a string:'); // This one won't make it past the "if" statement example([1, 2, '3', 4, 5, -2, 23, -1, -13, 10, -52]); console.log('With a boolean:'); // Neither will this one example([1, 2, false, 4, 5, -2, 23, -1, -13, 10, -52]);
By using of Array.some()
method you can easily find out if array contains any string value or not based on the type
checking.通过使用Array.some()
方法,您可以根据type
检查轻松找出数组是否包含任何字符串值。 It will return true if any of the element is string in the array else false.如果任何元素是数组中的字符串,它将返回 true,否则返回 false。
Live Demo :现场演示:
const arr = [1, 2, 3, 4, '5', -2, 23, -1, -13, 10, -52]; const isArrayContainsString = arr.some(elem => typeof elem === 'string'); console.log(isArrayContainsString); // true
Then, You can add this condition in your code :然后,您可以在代码中添加此条件:
if (isArrayContainsString)
return false
else
// remaining logic comes here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.