简体   繁体   English

比较一个数组与另一个数组以确保第二个数组不包含其他值

[英]compare one array to another to make sure the second doesn't contain a different value

I want to compare one array with another: 我想将一个数组与另一个数组进行比较:

array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'michael', 'bob'];

I want to detect whether array2 contains a name that isn't found in array1. 我想检测array2是否包含在array1中找不到的名称。

array2 can be longer or shorter than array1, in other words, array2 might be missing names or have more names than array1, but array2 cannot have a DIFFERENT name as compared to array1. array2可以长于或短于array1,换句话说,array2可能缺少名称或名称比array1多,但与array1相比,array2不能具有不同的名称。

So far, I can detect whether array2 is longer than array 1. If it is, it is obviously adding names and is therefore not valid: 到目前为止,我可以检测到array2是否比数组1长。如果是,则显然是在添加名称,因此无效:

if (array1.length < array2.length) {
      console.log('no');
}

but I this isn't as precise as it needs to be (if both arrays have an equal number of values, it returns true even if the individual vales don't correlate). 但是我并没有达到所需的精确度(如果两个数组具有相等数量的值,即使各个值不相关,它也会返回true)。

see the following for example scenarios: 请参阅以下示例场景:

array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'b', 'paul']; //should not be valid

array1 = ['billy', 'b', 'paul'];
array2 = ['billy', 'bob', 'paul']; //should not be valid

array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'michael', 'paul']; //should not be valid


array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'bob', 'paul', 'michael']; //should not be valid


array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'bob']; //this is valid


array1 = ['billy', 'bob', 'paul'];
array2 = ['billy']; //this is valid

array1 = ['bob', 'bob', 'billy', 'paul'];
array2 = ['paul', 'bob', 'bob', 'bob']; //this IS NOT valid

array1 = ['bob', 'bob', 'billy', 'paul'];
array2 = ['paul', 'bob', 'bob']; //this is valid

I'm assuming I should be using .every() but I am unsure as to how to implement it when comparing two arrays as all the examples I find test values of one array against a single value. 我假设我应该使用.every(),但是我不确定在比较两个数组时如何实现它,因为所有示例我都发现一个数组的测试值与单个值比较。

update: Array 2 cannot have more instances of a specific name than array 1, but it can have fewer. 更新:数组2不能具有比数组1更多的特定名称实例,但是可以更少。

This approach uses the function some to stop when at least one name is not in array1 and the sum of the names is not the same between the arrays. 当至少一个名称不在array1并且两个数组之间的名称之和不同时,这种方法使用some函数停止。

 let isValid = (arr, arr2) => { let sum = (array, n) => array.reduce((a, an) => a + (an === n), 0); return !arr2.some(n => { let sum2 = sum(arr2, n); return !arr.some(an => an === n && sum(arr, an) === sum2); }); }; console.log(isValid(['billy', 'bob', 'paul'], ['billy', 'b', 'paul'])); //should not be valid console.log(isValid(['billy', 'b', 'paul'], ['billy', 'bob', 'paul'])); //should not be valid console.log(isValid(['billy', 'bob', 'paul'],['billy', 'michael', 'paul'])); //should not be valid console.log(isValid(['billy', 'bob', 'paul'], ['billy', 'bob', 'paul', 'michael'])); //should not be valid console.log(isValid(['billy', 'bob', 'paul'], ['billy', 'bob'])); //this is valid console.log(isValid(['billy', 'bob', 'paul'], ['billy'])); //this is valid console.log(isValid(['bob', 'bob', 'billy', 'paul'], ['paul', 'bob', 'bob', 'bob'])); //this is NOT valid console.log(isValid(['bob', 'bob', 'billy', 'paul'], ['paul', 'bob', 'bob'])); //this is valid 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can use every to check if every object in array2 is in array1 : 您可以使用every检查array2中的每个对象是否在array1

 var array1 = ['billy', 'bob', 'paul']; var array2 = ['billy', 'michael', 'bob']; var allFound = array2.every(e => array1 .includes(e)); console.log(allFound); //Should return false because 'michael' is not in array1 

This also works the other way: 这也可以用另一种方式工作:

 var array1 = ['billy', 'bob', 'paul']; var array2 = ['billy', 'michael', 'bob']; var allFound = array1.every(e => array2.includes(e)); console.log(allFound); //Should return false because 'paul' is not in array2 

As suggested in the comments, you can also make array1 (the one which you want to check against - see first example) a Set , which is similar to an array but contains only unique values: 如注释中所建议,您还可以将array1 (要检查的对象-请参见第一个示例) SetSet ,它类似于数组,但仅包含唯一值:

 var array1 = ['billy', 'bob', 'paul']; var array2 = ['billy', 'michael', 'bob']; var array1Unique = new Set(array1); var allFound = array2.every(e => array1Unique.has(e)); console.log(allFound); //Should return false because 'michael' is not in array1 

If you want a simple and easy to understand solution, you don't need to use every. 如果您想要一个简单易懂的解决方案,则无需使用每一个。 Just use the code below. 只需使用下面的代码。

The code below uses a .forEach() loop to loop through array2 and, uses another .forEach() loop to find out if the value in array2 is also in array1 . 下面的代码使用.forEach()循环遍历array2并使用另一个.forEach()循环找出array2的值是否也在array1

Note: There are many more efficient methods, but this method is easier to understand if you're new to programming and want to understand how this is done. 注意:有许多更有效的方法,但是如果您不熟悉编程并且想了解如何完成此方法,则更易于理解。

Edit: As you suggested in the comments, I fixed my code so each value in array2 has only one counterpart in array1 . 编辑:正如您在注释中建议的那样,我修复了我的代码,因此array2每个值在array1只有一个对应项。 Not every time it matches the value from array2 and array1 , it removes the value from array1 . 并非每次它与array2array1中的值匹配时,都会从array1删除该值。

 var array1 = ['bob', 'bob', 'billy', 'paul']; var array2 = ['bob', 'bob', 'bob', 'billy']; var contains = false; var output = true; array2.forEach(e2 => { contains = false; array1.forEach(e1 => { if (e2 == e1) { contains = true; array1.splice(array1.indexOf(e1), 1); } }) if (!contains) { output = false; } }); console.log(output); 

You can do the task with a simple for loop and .includes() 您可以使用简单的for循环和.includes()完成任务

Example below: 下面的例子:

 var array1 = ['billy', 'michael', 'paul']; var array2 = ['billy', 'michael', 'bob']; var check = true; for(var i = 0; i < array2.length; i++){ if(!array1.includes(array2[i])){ check = false; break; } } console.log(check); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保一个项目不会两次被推入数组? - How to make sure an item doesn't get pushed into an array twice? 将一个值与另一个数组中的每个数组值进行比较 - compare one value with each value of array which is inside another array 如何仅在rethinkDB中不包含值的情况下追加到数组 - How to append to array only if it doesn't contain value with rethinkDB 使用JS的数组映射-将值与另一个数组比较并从第二个数组返回值 - Array Map using JS - Compare values to another array and return value from second array 如何确保所选文本不包含特定标记/类 - How to be sure selected text doesn't contain a specific tag/class 比较 2 arrays 并从一个数组到另一个数组查找属性值 - Compare 2 arrays and find an attribute's value from one array to another 确保TD输入值不超过以前的TD值 - Make sure TD input value doesn't exceed previous TD value 在 JavaScript 中如何确保数组至少有一个特定元素而其他元素满足另一个条件? - In JavaScript how to make sure that array has at least one specific element and the others meet another condition? 确保第一个 ajax 函数在第二个之前完成 - Make sure first ajax function finishes before second one .filter(_function) for arrays——我不确定为什么我的过滤器/函数的第一个版本有效但第二个版本无效 - .filter(_function) for arrays -- I'm not sure why the first version of my filter/function works but the second one doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM