Olivia
Olivia

Reputation: 195

check if an javascript array contains all of the values of another array

How to find if stringB contains all of the values of stringA?

Imagining you want to make a new string (stringB) out of stringA, when you take a value from stringA, it'll disappear from stringA, you can't use it twice.

So if stringB has repeated values, but stringA only has one of that value, then the program should return false.

Example Input:

stringA = "A B C D"

stringB = "B B C D"

Example Output:

false

Because stringA only has one "B".

Example Input:

stringA = "apple banana orange mango"

stringB = "banana orange"

Example Output:

true

Here is what I have, but it return true when it should've returned false can anyone tell me what is wrong with my logic or what should the solution be? Thanks!

let arrayContainsArray = (a, b) => {
  let a_array = a.split(" ")
  let b_array = b.split(" ")

  for(let i = 0; i < b_array.length; i++) {
    if (a_array.includes(b_array[i])) {
      let index = a_array.indexOf(b_array[i])
      a_array.splice(index, 1)
    } else {
      return false
    }
    return true
  }
}

console.log(arrayContainsArray('two times three is not four', 'two times two is four'));

Upvotes: 3

Views: 5364

Answers (3)

Jorge Kean
Jorge Kean

Reputation: 24

Sharing my approach using jquery each and inArray.

// created function
function existAll(baseArray, arrayToCompare) {
var result = true;       

$.each(baseArray, function (ix, data) {
    if (!($.inArray(data, arrayToCompare) !== -1)) {
        result = false;
        return false;
    }
});

return result;

}

// usage
alert(existAll([1,2,3,4],[1,2,3,5,4]));

Upvotes: 0

Kai
Kai

Reputation: 337

Your code does not work, because it always returns anything (if not false, then true) in first iteration (reason: return true statement is in for loop). Try this:

let arrayContainsArray = (a, b) => {
  let a_array = a.split(" ")
  let b_array = b.split(" ")

  for (let i = 0; i < b_array.length; i++) {
    if (a_array.includes(b_array[i])) {
      let index = a_array.indexOf(b_array[i])
      a_array.splice(index, 1)
    } else {
      return false
    }
  }
  return true
}

console.log(arrayContainsArray('two times three is not four', 'two times two is four'));
console.log(arrayContainsArray('A B C D', 'B B C D'));
console.log(arrayContainsArray('apple banana orange mango', 'banana orange'));

Upvotes: 2

Eddie
Eddie

Reputation: 26844

You can use every to loop thru the array and check the condition and includes to check if an array contains a certain element.

let arrayContainsArray = (a, b) => {
  let a_array = a.split(" ")
  let b_array = b.split(" ")

  return a_array.every(o => b_array.includes(o));
}

console.log(arrayContainsArray('two times three is not four', 'two times two is four'));
console.log(arrayContainsArray('two times two is four', 'two times two is four'));


Another option is to make the b_array into a new Set. Use has to check if a set includes a certain element.

let arrayContainsArray = (a, b) => {
  let a_array = a.split(" ")
  let b_array = b.split(" ")

  let set = new Set(b_array);
  return a_array.every(o => set.has(o));
}

console.log(arrayContainsArray('two times three is not four', 'two times two is four'));
console.log(arrayContainsArray('two times two is four', 'two times two is four'));

Upvotes: 3

Related Questions