[英]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
(要检查的对象-请参见第一个示例) Set
为Set
,它类似于数组,但仅包含唯一值:
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
. 并非每次它与array2
和array1
中的值匹配时,都会从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.