I have an array arrayDescr and I need to use forEach to add to every <li> an attribute data-value. The value of data-value should be an arrayDescr element. For example I have three <li>'s and I have two arrayDescr elements. Two of three <li>'s should have attribute data-value arrayDescr[0], [1] and etc.
This code works but I need it to work in forEach:
$("#name1").attr("data-value", arrayDescr[0])
#name should be function(){return 'name' (i+1)}.
In forEach this code returns undefined:
arrayDescr.forEach(function(item, index){
$("li").attr("data-value", item[index])}
.forEach, the function parameteritemis a single item of the array, that's why you get undefined by thisitem[index]