I have a Array of classes, its a dynamic Array which can have n number of class names like this.
let arr = ['Product', 'year', 'role'];
and then I have lots of elements with these classes inside a parent div like this.
<div class="parent">
<div class="year"></div>
<div class="abc"></div>
<div class="Product"></div>
<div class="diffClass"></div>
<div class="role"></div>
<div></div>
</div>
Now in this structure I need to add class active on page load only on the div's which's class present in the Array. I tried to map the array but no table to get the divs with matching with Arrays class Thanks in advance.
You can select "or" classes using ,, eg
$(".product,.year,.role")
will select if any of them match
(conversely, to make them all match, use .product.year.role - that's not the requirement here)
To convert your array to a string, use arr.join, giving:
let arr = ['Product', 'year', 'role'];
$("." + arr.join(",.")).addClass("active");
.active { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parent">
<div class="year">year</div>
<div class="abc">abc</div>
<div class="Product">Product</div>
<div class="diffClass">diffClass</div>
<div class="role">role</div>
<div></div>
</div>