[英]How to check if two elements inside two array have the same index?
我是 javascript 的新手,想知道是否有可能的方法来查明数组中的元素是否与另一个数组中的另一个元素具有相同的索引。
例如:
var a = [4,6,3]
var b = [6,6,0]
如何确定 arrays 中索引 1 处的元素在 Javascript 中是否相同或使用 jQuery?
我无法理解这一点,我们将不胜感激任何帮助。
在学习那些简单易用的方法之前,我个人认为你应该先学会一个非常基本的方法来解决它
let index = -1; // -1 normally defined as not found
for (let i = 0 ; i < a.length ; i++) {
for (let j = 0 ; j < b.length ; j++) {
if (a[i] === b[j]) {
index = i; // or index = j; is the same
} // we won't need else since we don't need to do anything when the numbers are not match
}
}
既然你要求“神奇”的 JS 方式,
a.some((i,j) => i == b[j])
应该按照你的要求去做。
some
命令对数组的每个元素运行 function 并检查它们是否为真。 function 参数i
是第一个数组中元素的值, j
是索引。 我们使用j
获取b
在该点的值并检查它是否相等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.