[英]Compare same index of two array having same length
I have two arrays with same length我有两个长度相同的数组
array1 = [12,13,14,15,16]
array2 = [22,32,45,11,7]
Now I want to compare only the same index value in a for loop in javasacript ex:现在我只想比较 javasacript ex 中 for 循环中相同的索引值:
array1[0] < array2[0]
array1[1] < array2[1]
array1[2] < array2[2]
and if value of array1[index]
is less than array2[index]
it return true
如果
array1[index]
值小于array2[index]
则返回true
I suppose the output must be a boolean
array.我想输出必须是一个
boolean
数组。 For this, you can use map
method by passing a callback
function.为此,您可以通过传递
callback
函数来使用map
方法。
array1 = [12,13,14,15,16] array2 = [22,32,45,11,7] result = array1.map(function(item, i){ return item < array2[i]; }); console.log(result);
or simply using arrow
functions.或者简单地使用
arrow
函数。
array1 = [12,13,14,15,16], array2 = [22,32,45,11,7] result = array1.map((item, i) => item < array2[i]); console.log(result);
If you want to return 1
or 0
values you can use +
operator in order to force
the result to number primitive type.如果您想返回
1
或0
值,您可以使用+
运算符来force
结果为数字原始类型。
result = array1.map(function(item, i){
return + (item < array2[i]);
});
If you want to receive array with boolean values which indicates if values are lower or higher try this:如果您想接收带有布尔值的数组,该值指示值是较低还是较高,请尝试以下操作:
var result = [];
for(var i = 0; i < array1.length;i++){
result.push(array1[i] < array2[i]);
}
Try this:试试这个:
array1 = [12,13,14,15,16];
array2 = [22,32,45,11,7];
for(var x = 0; x < array1.length; x++) {
if(array1[x] < array2[x]) {
//do something
}
}
If I understood what you mean with your question this is called lexicographical comparison and is built-in for arrays in other languages (for example Python or C++).如果我理解您的问题的意思,这称为词典比较,并且是其他语言(例如 Python 或 C++)中内置的数组。
In Javascript is present for strings, but you've to implement it yourself for arrays with something like在 Javascript 中存在字符串,但您必须自己为数组实现它,例如
// lexical less-than-or-equal
function lex_leq(a, b) {
let i = 0, na = a.length, nb = b.length, n = Math.min(na, nb);
while (i<n && a[i] === b[i]) i++; // skip over equals
return i === na || (i !== nb && a[i] < b[i]);
}
Other orderings can be computed by similar code or reusing the same implementation... for example (a<b)
is the equivalent to !(b<=a)
.其他排序可以通过类似的代码或重用相同的实现来计算......例如
(a<b)
等价于!(b<=a)
。
a1 = [12,13,14,15,16]
a2 = [22,32,45,11,7]
result = a1.map(function(item, i){
return item < a2[i];
});
console.log(result);
Try this solution.试试这个解决方案。 This will tell you which index doesn't match.
这将告诉您哪个索引不匹配。
var array1 = [12,13,14,15,16]; var array2 = [12,13,14,15,34]; function compareArray(arr1, arr2) { if(arr1.length !== arr2.length) return false; return arr1.reduce(function(acc, val, index) { acc.push(val === arr2[index]); return acc; }, []); } var response = compareArray(array1, array2); console.log(response);
array1 = [12,13,14,15,16] array2 = [22,32,45,11,7] result = [] array1.map(function(item, i){ result[i] = item < array2[i]; }); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.