[英]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.