-1
// HTML

// Some Html content

<div class=​"class--1 common-class otherClass2">​…​</div>​

// Some Html content
<div class="otherdiv"></div>

<div class=​"common-class class--2">​…​</div>​
<div class=​"common-class class--3 otherClass">​…​</div>​

// Some Html content

i'm trying to iterate between 2 arrays; one with className stored and the other with html elements targeted

// JAVASCRIPT 
var arrayOfClasses  = ["class--5", "class--10", "class--1", "class--2"];

    var arrayOfElements = document.getElementsByClassName("common-class");
    // This return the following array
    // [<div class=​"common-class class--1">​…​</div>​, <div class=​"common-class class--2>​…​</div>​, <div class=​"common-class class--3>​…​</div>​];

How i get true value for the element with class--1 and class--2 and false for class--3 class--5 and class--10

I iterate throw the arrayOfElements[i].className and after use the indexOf commented below but found another problem here :S

"common-class class--10".indexOf("class--1")
// Return > 13     but  i want -1 because is 10 no 1  

Better without jQuery Thanks in advance.

Héctor León
  • 2,210
  • 2
  • 23
  • 36

1 Answers1

0

var array1 = ["Hello", 5, "Hola", 27, 'otherValue'];
var array2 = [27, "Hello"];

var intersection = [];

var intersectionObj = {};

for(var i=0; i<array1.length; i++) {
  if(array2.indexOf(array1[i]) > -1) {
    intersection.push(array1[i]);
  }
}
alert(intersection);
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  
</body>
</html>
isambitd
  • 829
  • 8
  • 14