Nodejs Array Subset isSubsetOf(array)

Here you can find the source of isSubsetOf(array)

Method Source Code

/*/*from  w w  w.j a v a2  s .  c o  m*/
 * Make an array method that can return whether or not a context array is a
 * subset of an input array.  To simplify the problem, you can assume that both
 * arrays will contain only strings.
 *
 *
 * var a = ['commit','push']
 * a.isSubsetOf(['commit','rebase','push','blame']) // true
 *
 * NOTE: You should disregard duplicates in the set.
 *
 * var b = ['merge','reset','reset']
 *
 * b.isSubsetOf(['reset','merge','add','commit']) // true
 *
 * See http://en.wikipedia.org/wiki/Subset for more on the definition of a
 * subset.
*/

/*
 * Extra credit: Make the method work for arrays that contain any value,
 * including non-strings.
*/

Array.prototype.isSubsetOf = function(array) {
  function arrayContains(array, item) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] === item) {
        return true;
      }
    }
    return false;
  }
  for (var i = 0; i < this.length; i++) {
    if (!arrayContains(array, this[i])) {
      return false;
    }
  }
  return true;
}

Related

  1. isSubsetOf(array)
    Array.prototype.isSubsetOf = function(array) {
      var isSubset = true;
      if(this.forEach(function(element){
        if(!array.includes(element)){
          isSubset = false;
      }));
      return isSubset;
    };
    ...
    
  2. isSubsetOf(array)
    Array.prototype.isSubsetOf = function(array) {
      var result = true;
      for (var i = 0; i < this.length; i ++) {
        if(array.indexOf(this[i]) === -1) {
          result = false;
      return result;
    };
    ...
    
  3. isSubsetOf(array)
    Array.prototype.isSubsetOf = function(array) {
      var arr = this;
      var obj = {};
      for(var i=0; i<array.length; i++) {
        obj[array[i]] = true; 
      for(var i=0; i<arr.length; i++) {
        if(!(arr[i] in obj)) {
          return false;
    ...
    
  4. isSubsetOf(array)
    Array.prototype.isSubsetOf = function (array) {
      var subset    = this;
      var subsetLen = subset.length;
      var supersetLen = array.length;
      var first, found;
      if ( subsetLen > supersetLen ) return false;
      if ( subsetLen === 0 && supersetLen >= 0 ) return true;
      first = subset[0];
      found = array.indexOf( first );
    ...
    
  5. isSubsetOf(array)
    Array.prototype.isSubsetOf = function(array) {
      var subArray = this;
      var map = {};
      for(var i=0;i<subArray.length;i++){
        if(!map[subArray[i]]){
          if(array.indexOf(subArray[i]) === -1){
            return false;
        map[subArray[i]] = true;
      return true;
    };
    
  6. isSubsetOf(array)
    var a = ['commit', 'push'];
    var b = ['merge', 'reset', 'reset'];
    Array.prototype.isSubsetOf = function (array) {
      for (var i = 0; i < this.length; i++) {
        if (array.indexOf(this[i]) === -1) {
          return false;
      return true;
    ...
    
  7. isSubsetOf(bigArray)
    Array.prototype.isSubsetOf = function(bigArray) {
      var uniqueSub = this.unique();
      var uniqueBig = bigArray.unique();
      var status = true;
      this.forEach(function(item) {
        if(bigArray.indexOf(item) === -1) status = false;
      });
      return status;
    };
    ...
    
  8. isSubsetOf(comparisonArray)
    Array.prototype.isSubsetOf = function(comparisonArray) {
      for (var i = 0; i < this.length; i++) {
        if (comparisonArray.indexOf(this[i]) === -1) {
          return false;
      return true;
    };
    
  9. isSubsetOf(input)
    Array.prototype.isSubsetOf = function(input) {
      var isSub = true;
      this.forEach(function (el) {
        if (!input.includes(el)) {
          isSub = false;
      });
      return isSub;
    };
    ...