Reputation: 27
I created an algorithm for displaying the title of an element in a certain block, but I can't delete what I output. js:
var products = document.querySelectorAll('.product-block'); //objects
var product_names = document.querySelectorAll('.product-block h5'); //headings which we should output
var array_product_names = Array.prototype.slice.call(product_names); //from NodeList to array
var favourite_elements = document.querySelector('.favourite_elements'); //block where we should output
//method of determining which button is pressed
products.forEach((product, i) => {
product.onclick = function () {
if (product.style.background != "orange") {
product.style.background = "orange";
var favourite_element = array_product_names[i].outerHTML;
favourite_elements.insertAdjacentHTML('afterbegin', favourite_element); //adding elements
}
else if (product.style.background == "orange") {
//here we should delete heading of determined object from list in block, but i don't know how
}
}
});
Upvotes: 0
Views: 389
Reputation: 331
This is oversimplified but it shows how you can remove items from a list:
let a=[1,2,3,4,5];
let toDelete=[2,3];
a=a.filter(e=>!toDelete.includes(e))
Now,the value of a would be:
[1,4,5]
Based on that idea, you could do more abstract things such as:
Array.prototype.filterA = function(f){
return this.filter(e=>f(e));
}
And from then on you would just filter elements like this:
[{color:"orange"},{a:1},{b:{c:2}}].filterA(e=>e.color!="orange")
Upvotes: 1