Javascript Array isSubsetOf(array)

Description

Javascript Array isSubsetOf(array)


Array.prototype.isSubsetOf = function(array) {
  return this.filter(item => array.find(x => x === item)).length == this.length
}

Javascript Array isSubsetOf(array)

Array.prototype.isSubsetOf = function(array) {
  for(var i = 0; i < this.length; i++){
    if(array.indexOf(this[i]) === -1){
      return false;
    }/*from   w  w  w  .  jav  a2 s  .c om*/
  }  
  return true;
};

Javascript Array isSubsetOf(array)

Array.prototype.isSubsetOf = function(array) {
  return this.reduce(function (acc, val) {
    if (acc) {//from   ww  w  .ja  va2s  .co m
      return array.includes(val);
    }
    return false;
  }, true)
};

Javascript Array isSubsetOf(array)

/*/*from   ww w.  ja v a 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) {
  let subset = true;
  this.forEach(function(element) {
    if (!array.includes(element)) {
      subset = false;
    }
  });
  return subset;
};

Javascript Array isSubsetOf(array)

/*//from w  w w .  j a v  a  2s.c o  m
Is Subset Of
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) {
  var isSubset = true;

  if(this.forEach(function(element){
    if(!array.includes(element)){
      isSubset = false;
    }
  }));

  return isSubset;
};

Javascript Array isSubsetOf(array)

// completed 8-26-2016

// 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.

// Solution //// ww w  .  ja va 2  s .co m

Array.prototype.isSubsetOf = function(array) {
  // iterate through the array that you are calling isSubsetOf on.
  // check to see if every item in that array is in the array that you are checking.
  return this.reduce((pre,cur) => (!pre) ? false : array.includes(cur),true);
};

Javascript Array isSubsetOf(array)

/*// w  ww.j  a v a 2  s  .  com
 * 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 = array => {
  let hash = {};
  let result = true;
  array.forEach(item => {
    hash.item ? hash.item++ : hash.item = 0;
  });
  this.forEach(item => {
    hash.item ? hash.item++ : result = false;
  });
  return result;
};



PreviousNext

Related