Jquery check if array 2 use all values array 1


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 why. Can someone help me? and if the problem does not come from there, anyone can tell me my mistake ?




function controlUserInput(inputText, appLang) {
const regex = /$[^$]*$/gm;
const str = $('#formulaire-preview-textarea').val();
let m;
var array = populateVariable(appLang);

while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
var isEqual = match.length==array.length;
// for (i=0; i<=array.length-1;i++){
if(displayCpt == 4 && isEqual && Array.from(match).every(function(paramInMatch){
return $.inArray(paramInMatch, array) != -1;
})){
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"
});
}
// }
})
};
}





To make sure, is your array variable an array of characters (forming a single String)? or is it an array of Strings?
– Rodrigo Ferreira
Jul 1 at 22:17



array





my array "match" get all values that the user has returned ( all values of textarea that are between $ $ ) and my second array "array" contain : $Champ 1$,$Champ 2$,$Tel$,$option 1$,$Optin 2$
– Willy Nguyen
Jul 1 at 22:32





That would be the problem, when you are iterating over m, a String is stored in match, then you are comparing a String vs an array of Strings
– Rodrigo Ferreira
Jul 1 at 22:37


m


match




1 Answer
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.


m


match


array



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


m


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];

function isSubSet(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];

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

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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?