Javascript Jquery检查数组2是否使用数组1的所有值

Javascript Jquery检查数组2是否使用数组1的所有值,javascript,jquery,html,css,jive,Javascript,Jquery,Html,Css,Jive,我试图检查array1是否使用我的array2的所有值,如果返回错误消息,但我的array1.length不等于array2.length,我正在搜索数小时以了解原因。有人能帮我吗?如果问题不是从那里来的,有人能告诉我我的错误吗 函数控制用户输入(inputText,appLang){ 常量正则表达式=/\$[^$]*\$/gm; 常量str=$(“#公式预览文本区域”).val(); 让m; var数组=populateVariable(appLang); while((m=regex.ex

我试图检查array1是否使用我的array2的所有值,如果返回错误消息,但我的array1.length不等于array2.length,我正在搜索数小时以了解原因。有人能帮我吗?如果问题不是从那里来的,有人能告诉我我的错误吗

函数控制用户输入(inputText,appLang){
常量正则表达式=/\$[^$]*\$/gm;
常量str=$(“#公式预览文本区域”).val();
让m;
var数组=populateVariable(appLang);
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
//可以通过'm`-变量访问结果。
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
var isEqual=match.length==array.length;
//对于(i=0;iEdit:
当您尝试迭代
m
的所有元素时,单个字符串存储在
match
中,因此当您尝试与
数组进行比较时,它会失败

解决方案不是迭代
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'
  });
}
希望有帮助

原始答案 如果要检查数组的每个元素是否在另一个数组中使用,可以使用两种方法:

Arr2是Arr1的子集 Arr2包含Arr1的所有元素
为确保这一点,您的
array
变量是字符数组(形成单个字符串)?还是字符串数组?my array“match”获取用户返回的所有值(textarea中介于$$之间的所有值)和my second array“array”contain:$Champ 1$,$Champ 2$,$Tel$,$option 1$,$Optin 2$这将是问题所在,当您在
m
上迭代时,字符串存储在
match
中,然后您将字符串与字符串数组进行比较
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
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