I am new to javascript and want to know if there is a possible method to find out if an element inside an array has the same index as another element inside another array.
For example :
var a = [4,6,3]
var b = [6,6,0]
how can I find out if the elements at index 1 in both arrays are the same in Javascript or using jQuery?
I cannot figure this and and any help would be deeply appreciated.
2
Answers
Before learning those easy-to-use methods, I’m personally think you should learn to solve it in a very basic way first
Since you asked for the "magical" JS way,
a.some((i,j) => i == b[j])
should do what you’re asking.The
some
command runs a function across every element of the array and checks if any of them are true. The function parametersi
is the value of the elements in the first array andj
is the index. We use thej
to get the value ofb
at that point and check if it’s equal.