Apostolos
Apostolos

Reputation: 8111

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

I am trying to check if all my values are defined in an array of mine. My code is the following

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


array.every(check);

I tried it on the following arrays:

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

What am I doing wrong?

Upvotes: 5

Views: 3499

Answers (5)

Apostolos
Apostolos

Reputation: 8111

Because the lenght of the array is predetermined I managed to bypass this by using filter function.

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

Thank you all for answering!!As always great community

Upvotes: 0

dReAmEr
dReAmEr

Reputation: 7196

Try below.

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());

Upvotes: 0

Marco Bonelli
Marco Bonelli

Reputation: 69367

That's because the .every() method checks if the value exists, before calling your check() function, so check() will only be called for the last element (0).

NOTE: Remember also that the .every() method stops if your function returns a false value.

Try this instead, if you want to check:

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

Upvotes: 0

Amir Popovich
Amir Popovich

Reputation: 29846

As said above every skips "holes".

If you really want this functionality then you can add this simple method:

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

    return true;
}

Upvotes: 4

Scimonster
Scimonster

Reputation: 33409

MDN provides a polyfill (read: the exact code) for Array.every. If we just modify this to remove the check that the property exists, it's easy:

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;
  };
}

I've just renamed it to Array.reallyEvery().

Upvotes: 0

Related Questions