Nodejs Array Extract extract(predicate)

Here you can find the source of extract(predicate)

Method Source Code

// Array Extract - Based on work By John Resig (MIT Licensed)
// http://ejohn.org/blog/javascript-array-remove/
// Removes items matching predicate and returns removed items
// This function retains the integrity of the original array
Array.prototype.extract = function(predicate) {
    var removed = [];

    //test whether this is necessary
    if (this.length===0 || !predicate || typeof predicate !== 'function') {
      return removed;
    }/*  w w  w  . j av a2  s  .c  o m*/

    var idx = 0;
    while(idx < this.length) {
        if (predicate(this[idx])) {
            removed.push(this[idx]);
            var rest = this.slice(idx + 1 || this.length);
            this.length = idx < 0 ? this.length + idx : idx;
            this.push.apply(this, rest);
        } else {
            idx++;
        }
    }
    return removed;
};

Related

  1. extract(attribute)
    "use strict";
    Array.prototype.extract = function(attribute) {
      var newArr = [];
      for (var i = 0; i < this.length; i++) {
        newArr.push(this[i][attribute]);
      return newArr;
    
  2. extract(field)
    Array.prototype.extract = function(field) {
      var newArray = [];
      for (var i = 0; i < this.length; i++) {
        if (this[i][field] != undefined) {
          newArray.push(this[i][field]);
      return newArray;
    };
    ...