Javascript Array containsArray(array)

Description

Javascript Array containsArray(array)


/**//  ww w .  jav  a  2 s . co m
  * @jsx React.DOM
*/
// Using this code to be able to compare two arrays, able to using containsArray() method.
Array.prototype.containsArray = function ( array /*, index, last*/ ) { //http://jsfiddle.net/ThinkingStiff/X9jed/
    if( arguments[1] ) {
        var index = arguments[1], last = arguments[2];
    } else {
        var index = 0, last = 0; this.sort(); array.sort();
    };
    
    return index == array.length
        || ( last = this.indexOf( array[index], last ) ) > -1
        && this.containsArray(array, ++index, ++last );
};

Javascript Array containsArray(array)

Array.prototype.containsArray = function(array){
    if(!this[0] || !this[0][0]) console.log('This function only works on 2 dimensional arrays!');
    for(let i=0; i<this.length; i++){
      if(this[i].sort().join(',') === array.sort().join(',')){
        return true
      }//from   www .  j  ava  2 s  . c  o m
    }
    return false;
  }



PreviousNext

Related