Nodejs Array Intersect intersect(array)

Here you can find the source of intersect(array)

Method Source Code

/* Should join two arrays if they are matching with (this.length-1) elements.
 * The parameter array should be bigger or have equal length at least => [1,2,3] and [2,3,4]. */

Array.prototype.intersect = function(array){
  if(this.length>array.length) return '';
  var equals = true,
      concat = '';
  for(var i = 0; i < this.length-1; i++){
    if(this[i+1] != array[i]){
      equals = false;/*  w w w .  j  a  v  a2  s  . c o  m*/
      break;
    }
  }
  if(equals){
    concat = this.reduce((acc,k,i) => acc + (i<this.length?k:''), '');
    concat = array.reduce((acc,k,i) => acc + (i>=this.length-1?k:''), concat);
    return concat;
  }
  return '';
}

Related

  1. intersect(arr)
    Array.prototype.intersect = function (arr) {
      return this.filter(function (el, idx) {
        return arr.indexOf(el) >= 0;
      }).unique();
    };
    
  2. intersect(arr, comparer)
    Array.prototype.intersect = function (arr, comparer) {
      comparer = comparer || EqualityComparer;
      return this.distinct(comparer).where(function (t) {
        return arr.contains(t, comparer);
      });
    };
    
  3. intersect(array)
    Array.prototype.intersect = function (array) {
        array = Array.isArray(array) ? array : [];
        return this.filter(function (n) {
            return array.indexOf(n) != -1;
        });
    };
    
  4. intersect(array)
    Array.prototype.intersect = function(array){
      return(this.filter(function(n){ return array.include(n); }));
    };
    
  5. intersect(array)
    Array.prototype.intersect = function(array) {
      return this.filter((x) => array.indexOf(x) != -1);
    };
    
  6. intersect(ary)
    Array.prototype.intersect = function (ary) {
        return this.union(ary).filter(x => this.contains(x) && ary.contains(x));
    };
    
  7. intersect(b)
    Array.prototype.intersect = function(b) {
      var array = new Array();
      var ua = this.uniquelize();
      var length = ua.length;
      for (var i = 0; i < length; i++) {
        if (b.inArray(ua[i])) {
          array.push(ua[i]);
      return array;
    };
    
  8. intersect(b)
    Array.prototype.intersect = function(b) {
        var flip = {}, res = [];
        for (var i = 0; i < b.length; i++) flip[b[i]] = i;
        for (i = 0; i < this.length; i++)
            if (flip[this[i]] != undefined) res.push(this[i]);
        return res;
    
  9. intersect(tab)
    Array.prototype.intersect = function(tab){
        var temp = [];
        for(var i = 0; i < this.length; i++){
            for(var k = 0; k < tab.length; k++){
                if(this[i] == tab[k]){
                    temp.push( this[i]);
                    break;
        return temp;
    };