简体   繁体   English

使用 Number.isInteger() 方法得到错误的结果,javascript

[英]Getting wrong result with Number.isInteger() method, javascript

I've been trying the Number.isInteger() method on chrome console.我一直在 Chrome 控制台上尝试 Number.isInteger() 方法。 and after doing a for loop and checking the result with the console.log(arr);并在执行 for 循环并使用console.log(arr);检查结果之后console.log(arr); I'm getting an array with only one value of 1. like this [1];我得到一个只有一个值为 1 的数组。像这样[1];

 var arr = [1,2,3,'some','string'];

for (var i = 0; i < arr.length; i++) {
    if (Number.isInteger(arr[i])) {
        arr.splice(arr.indexOf(arr[i], 1));
    }
}

Any one have an idea, if I'm doing it wrong or something.任何人都有一个想法,如果我做错了什么。 thanks for help.感谢帮助。

You have a major problem, you are continually removing items from the array while looping through it.您有一个主要问题,您在循环遍历数组时不断从数组中删除项目。 You have to go back one step ( i-- ) every time an item is removed.每次删除项目时,您都必须返回一步 ( i-- )。

 var arr = [1,2,3,'some','string']; for (var i = 0; i < arr.length; i++) { if (!isNaN(arr[i])) { // isNaN return true if it's not a valid number, so we have to inverse the test arr.splice(i, 1); // if it's a valid number remove the element at the index i (no need to search for the index using indexOf, we already have it its i) i--; // if we remove an element, we have to go back one step (or we will leave one item behind every time we remove another) } } console.log(arr);

You could use typeof instead:您可以使用 typeof 代替:

var arr = [1,2,3,'some','string'];
for (var i = 0; i < arr.length; i++) {
  if (typeof arr[i] == 'number') {
    arr.splice(arr.indexOf(arr[i], 1));
  }
}

The `.splice()' method makes changes to the array itself. `.splice()' 方法对数组本身进行更改。

So for each iteration of the array, you are changing it fundamentally.因此,对于数组的每次迭代,您都在从根本上改变它。

If you want the array to include only integers:如果您希望数组仅包含整数:

    var arr = [1,2,3,'some','string'];

    var newArray = [];

    arr.forEach(function(element) {
        if (Number.isInteger(element)){
            newArray.push(element);
        }
    });

    console.log(newArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中修复Number.isInteger()函数 - How to fix Number.isInteger() function in JavaScript 与 Number.isInteger() 苦苦挣扎 - struggling with Number.isInteger() Number.isInteger(this) 在 Number.prototype 方法中不起作用 - Number.isInteger(this) doesn't work in Number.prototype method if.IsInteger()在if else语句中不起作用 - Number.IsInteger() in if else statement not working 在以下Javascript数组方案中,parseInt()和Number.isInteger()之间的功效差异? - Difference in efficacy between parseInt() and Number.isInteger() in following Javascript array scenario? 我还能用什么来代替 Number.isInteger? - What else can i use instead of Number.isInteger? 创建的Number.isInteger(x)无法在IE中工作 - Number.isInteger(x) which is created can not work in IE 我需要检查一个数字是否不是小数,我正在尝试使用 Number.isInteger() 但不起作用 - I need to check if a number is not a decimal, I'm trying with Number.isInteger() but doesn't work 在使用 Number.isInteger 检查 object 属性是 integer 后,object 怎么可能为 null/undefined? - How can object be null/undefined after checking that object property is an integer using Number.isInteger? Javascript - 用于检查数组元素上的 isInteger 的 every() 方法 - Javascript - every() method to check isInteger on array elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM