Good evening, how can I check the elements of two arrays in javascript?

We have one array, full of links. Assume:

var one=['mail.ru','google.com','yandex.ru','rambler.ru']; 

And another, 'dynamic' array with at least 1, maximum 3 elements, depending on the situation .. Also contains links.

 var two=['google.com','rambler.ru']; //ΠΏΡ€ΠΈΠΌΠ΅Ρ€ с 2-мя Π΅Π»Π΅ΠΌΠ΅Π½Ρ‚Π°ΠΌΠΈ 

It is necessary to somehow check whether the elements of the second array coincide with the first, and if so (I have 2 elements in the example), let them know what, for example, the indexes of the elements in the 1st array.

Tried to do something through the usual for and if, but got confused = \

  • one
    Minus, there is a task, but there are no signs of independent attempts at solving. A trivial task, by the way. - karmadro4

2 answers 2

 var idx=0; for (var i=0;i<two.length;i++) { idx=one.indexOf(two[i]); if (idx>=0) alert(idx); } 

    option using underscore.js :

     _.each(['mail.ru','google.com','yandex.ru','rambler.ru'], function(url, idx){ if(_.include(['google.com','rambler.ru'], url)){ alert(idx); } }); // ΠΈΠ»ΠΈ _.intersection(['mail.ru','google.com','yandex.ru','rambler.ru'], ['google.com','rambler.ru']); // Π²Π΅Ρ€Π½Ρ‘Ρ‚ пСрСсСчСниС элСмСнтов массивов, Ρ‚.Π΅. ['google.com','rambler.ru'] 
    • four
      Little jQuery :-) - karmadro4
    • 2
      Yet for such tasks, the base JS is enough, IMHO - Bandicoot87