Nodejs Array Subset isSubsetOf(array)

Here you can find the source of isSubsetOf(array)

Method Source Code

// Is Subset Of/*  ww w  .  ja va  2 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.

Array.prototype.isSubsetOf = function(array) {
  // set result to true
  var result = true;
  // loop through this
  for (var i = 0; i < this.length; i ++) {
      // check if array[i] is equal to this
    if(array.indexOf(this[i]) === -1) {
      // if not found, result is false
      result = false;
    }
  }
  // return result
  return result;
};

Related

  1. isSubsetOf(arr)
    Array.prototype.isSubsetOf = function (arr) {
      var subArr = this;
      var words = {};
      for (var i = 0; i < arr.length; i++) {
        if (!words[arr[i]]) {
          words[arr[i]] = true;
      for (var i = 0; i < subArr.length; i++) {
    ...
    
  2. isSubsetOf(arr)
    Array.prototype.isSubsetOf = function (arr) {
      var objectify = function (arr) {
        var obj = {};
        for (var i = 0; i < arr.length; i++) {
          obj[arr[i]] = true;
        return obj;
      };
      var obj = objectify(arr);
    ...
    
  3. isSubsetOf(array)
    Array.prototype.isSubsetOf = function(array) {
      return this.reduce(function (acc, val) {
        if (acc) {
          return array.includes(val);
        return false;
      }, true)
    };
    
  4. isSubsetOf(array)
    Array.prototype.isSubsetOf = function(array) {
      for(var i = 0; i < this.length; i++){
        if(array.indexOf(this[i])===-1){
          return false
      return true;
    };
    
  5. isSubsetOf(array)
    Array.prototype.isSubsetOf = function(array) {
      var isSubset = true;
      if(this.forEach(function(element){
        if(!array.includes(element)){
          isSubset = false;
      }));
      return isSubset;
    };
    ...
    
  6. 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;
    ...
    
  7. 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 );
    ...
    
  8. 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;
    };
    
  9. isSubsetOf(array)
    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;