Nodejs Array Except except(cleanIndex)

Here you can find the source of except(cleanIndex)

Method Source Code

// Extend the array object with a function to return all elements of that array, except the ones with the
// indexes passed in the parameter.
// For example:/*from   w  w  w  .  j  a  v a  2s  . c om*/
// var array = ['a', 'b', 'c', 'd', 'e'];
// var array2 = array.except([1,3]); => ['a', 'c', 'e']
// The function should accept both array as parameter but also a single integer, like this:
// var array = ['a', 'b', 'c', 'd', 'e'];
// var array2 = array.except(1);
// array2 should contain ['a', 'c', 'd', 'e'];

Array.prototype.except = function (cleanIndex) {
  return this
    .filter((value, index) =>
      (typeof cleanIndex === 'object')
        ? cleanIndex.indexOf(index) === -1
        : index !== cleanIndex);
}

console.log(
  ['a', 'b', 'c', 'd', 'e'].except([0, 1]), // => [ 'c', 'd', 'e' ]
  ['a', 'b', 'c', 'd', 'e'].except(1) // => [ 'a', 'c', 'd', 'e' ]
);

Related

  1. except(arr, comparer)
    Array.prototype.except = function (arr, comparer) {
        if (!(arr instanceof Array)) arr = [arr];
        comparer = comparer || EqualityComparer;
        var l = this.length;
        var res = [];
        for (var i = 0; i < l; i++) {
            var k = arr.length;
            var t = false;
            while (k-- > 0) {
    ...
    
  2. except(ary)
    Array.prototype.except = function (ary) {
        var result = new Array();
        this.forEach(x => {
            if (!ary.contains(x))
                result.push(x);
        });
        return result;
    };
    
  3. except(filter)
    Array.prototype.except = function(filter) {
      var elements = this.filter(function(element) {
        return element != filter
      });
      return elements
    
  4. except(keys)
    Array.prototype.except = function(keys)
      let arr = this.map((i) => i);
      if(keys.length) {
        keys.sort((a,b) => a - b);
        keys.forEach((k, i) => { arr.splice(k - i, 1);});
      } else {
        arr.splice(keys, 1);
      return arr;
    
  5. except(keys)
    var array = [1,2,3,4,5,6];
    Array.prototype.except = function (arr) {
      var myArray = this.map((i) => i);
      (isFinite(arr)) ? myArray.splice(arr, 1) : arr.sort((a,b) => a - b).map((el, i) => {if (i != 0) el -= 1; myArray.splice(el, 1);});
      return myArray;
    console.log(array.except([1,3]));
    console.log(array);