jQuery检查数组2是否使用数组1的所有值 [英] Jquery check if array 2 use all values array 1

查看:73
本文介绍了jQuery检查数组2是否使用数组1的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查array1是否使用了我的array2的所有值,如果返回错误消息,但我的array1.length不等于array2.length,我正在寻找数小时以了解原因.有人能帮我吗?如果问题不出在那儿,谁能告诉我我的错误?

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"
				});
			}	
		// }
	})
    };
}

推荐答案

当您尝试遍历m的所有元素时,单个字符串存储在match中,因此当您尝试与array进行比较时,它将失败.

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的所有元素,而是:

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'
  });
}

希望有帮助!

如果要检查数组的每个元素是否在另一个数组中使用,则可能有两种方法:

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

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包含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

这篇关于jQuery检查数组2是否使用数组1的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆