使用Array.every()函数检查是否定义了所有值

Using Array.every() function to check if all values are defined

本文关键字:定义 是否 检查 every Array 函数 使用      更新时间:2023-09-26

我正在尝试检查我的所有值是否都定义在我的数组中。我的代码是以下

var check = function (item){return item !== undefined;};

array.every(check);

我在以下阵列上尝试过:

var array = [];
array[5] = 0; //[undefined × 5, 0]
array.every(check); //return true although there are 5 udefined values there

我做错了什么?

如上所述,every跳过"孔"。

如果你真的想要这个功能,那么你可以添加这个简单的方法:

Array.prototype.myEvery= function (pred) {
    for (var i = 0; i < this.length; i++) {
        if (!pred(this[i])) return false;
    }
    return true;
}

MDN为Array.every提供了polyfill(读取:确切的代码)。如果我们只是修改它以删除对属性存在的检查,这很容易:

if (!Array.prototype.reallyEvery) {
  Array.prototype.reallyEvery = function(callbackfn, thisArg) {
    'use strict';
    var T, k;
    if (this == null) {
      throw new TypeError('this is null or not defined');
    }
    // 1. Let O be the result of calling ToObject passing the this 
    //    value as the argument.
    var O = Object(this);
    // 2. Let lenValue be the result of calling the Get internal method
    //    of O with the argument "length".
    // 3. Let len be ToUint32(lenValue).
    var len = O.length >>> 0;
    // 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
    if (typeof callbackfn !== 'function') {
      throw new TypeError();
    }
    // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
    if (arguments.length > 1) {
      T = thisArg;
    }
    // 6. Let k be 0.
    k = 0;
    // 7. Repeat, while k < len
    while (k < len) {
      var kValue;

        // i. Let kValue be the result of calling the Get internal method
        //    of O with argument Pk.
        kValue = O[k];
        // ii. Let testResult be the result of calling the Call internal method
        //     of callbackfn with T as the this value and argument list 
        //     containing kValue, k, and O.
        var testResult = callbackfn.call(T, kValue, k, O);
        // iii. If ToBoolean(testResult) is false, return false.
        if (!testResult) {
          return false;
        }
      k++;
    }
    return true;
  };
}

我刚刚把它重命名为Array.reallyEvery()

这是因为.every()方法在调用check()函数之前会检查该值是否存在,因此check()将只为最后一个元素(0)调用。

注意:请记住,如果函数返回false值,则.every()方法将停止。

如果你想检查,试试这个:

var array = [1, 2, undefined, undefined, 1];
var check = function (item){return item !== undefined;};
array.every(check)
// this will work for 1 and 2, but will stop at the third element
array.every(function(item) {console.log(item); return true;});
// this will log all the elements, because the function always returns true

请尝试以下操作。

Array.prototype.every = function(){
  for(var i=0;i<this.length;i++){
   if(this[i]==undefined)
    return false;
  }
  return true;
 }
var array = [];
array[5] = 0;
console.log(array.every());

因为数组的长度是预先确定的,所以我使用filter函数绕过了它。

var check = function (item) {return item !== undefined};
array.filter(check).length === predeterminedLength;

谢谢大家的回答!!一如既往的伟大社区