简体   繁体   English

如果数组在javascript中具有索引的字符串值,如何获取错误

[英]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.

相关问题 Javascript通过将字符串值与数组进行比较来获取数组的索引值 - Javascript Get index value of array by comparing string value to array 如何获取数组中最大值的索引? 的JavaScript - How to get the index of the largest value in an array? JavaScript 如何在 JavaScript 中的特定数组索引处获取值? - How to get value at a specific index of array In JavaScript? 如何在基于数组javascript的索引上获取价值 - how to get value on array javascript based index 在JavaScript中获取具有给定字符串值的多维数组的索引 - Get the index of a multidimensional array with the value of a given string in javascript 在javascript数组中按值获取索引 - get index by value in javascript array 如何在 javascript 中获取相同数组值的索引值 - How to get index value of same array value in javascript 如何正确检查索引是否存在,然后使用index获取值,然后从数组javascript / jquery中删除索引和值 - How to properly check if index exists, then use index to get value, then delete index & value from array javascript/jquery 如何使用 javascript 从对象数组中查找其中一项是否已完成属性值为 false 并做出反应? - How to find if one of the items have completed property with value false from an array of objects using javascript and react? 如何在javascript中使用数组的值获取数组的索引? - How to get index of array using its value in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM