Skip to content Skip to sidebar Skip to footer

Jquery Check If Array 2 Use All Values Array 1

i'm trying to check if the array1 use all values of my array2, if false return error message, but my array1.length is not equal to array2.length , i'm searching for hours to know w

Solution 1:

Edit:

When you are trying to iterate over all the elements of m, a single String is stored in match so when you are trying to compare with array it fails.

Instead of iterating over all the elements of m, the solution would be:

if (
  m.length === array.length &&
  m.every(el => array.includes(el))
) {
  osapi.jive.core.container.sendNotification({
    message: 'Toutes les valeurs rentrées sont correctes',
    severity: 'success'
  });
} else {
  osapi.jive.core.container.sendNotification({
    message: "Vous n'avez pas utilisé toutes les valeurs",
    severity: 'error'
  });
}

Hope it helps!

Original Answer

If you want to check if every element of an array is used in another array you may have two approaches:

Arr2 is a SubSet of Arr1

const arr1 = [1, 2, 3, 4, 5, 6];
const arr2 = [1, 2, 5, 6];

functionisSubSet(subSet, wholeSet) {
  return subSet.every(el => wholeSet.includes(el));
}

console.log(isSubSet(arr2, arr1)); // returns true

Arr2 contains all elements of Arr1

const arr1 = [1, 2, 3, 4, 5, 6];
const arr2 = [1, 2, 5, 6];
const arr3 = [1, 2, 5, 6, 3, 4];

functionisWholeSet(subSet, wholeSet) {
  return (
    subSet.length === wholeSet.length &&
    subSet.every(el => wholeSet.includes(el))
  );
}

console.log(isWholeSet(arr2, arr1)); // returns falseconsole.log(isWholeSet(arr3, arr1)); // returns true

Post a Comment for "Jquery Check If Array 2 Use All Values Array 1"