What would be the equivalent of this piece of code in pure JS (without jQuery)?
$.each(data, function(i, item) {
if(window.location.href.indexOf(data[i].url) > -1) {
slt = data[i].id;
if ($(slt).length) {
$(slt).html(data[i].html);
}
}
})
I tried this example without a success.
var elements = document.querySelectorAll(data);
Array.prototype.forEach.call(elements, function(el, i){
alert(el);
});
Could you please help me ?
forEach() is an array function which loops over your array elements and provides data element and it's index in callback.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
call() -- called on a and is used to execute a function.
Solution for your question assuming data is some array.
data.forEach(function(item,i){
if(window.location.href.indexOf(data[i].url) > -1){
slt = data[i].id;
if(slt.length)
{
document.getElementById(slt).innerHTML = data[i].html;
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.