I'm trying to remove specific element from a array in javascript, but, when I use the splice method, it deletes the whole array, instead of the specific element.
Code:
const cart = {
contents: [],
addItem(item) {
cart.contents.push(item);
},
removeItem(item) {
let searchItem = this.contents;
if (searchItem.includes(item, 0)) {
searchItem.splice(item);
console.log(`${item} successfully removed from cart`);
} else {
console.log(`You tried to remove ${item} from your cart, but ${item} is not in the cart`);
};
}
};
cart.addItem("laptop");
cart.addItem("pen");
cart.addItem("book");
console.log("The cart contains:", cart.contents);
cart.removeItem("laptop");
console.log("The cart contains:", cart.contents);
the console log:
"The cart contains:" ["laptop", "pen", "book"] <br>
"laptop successfully removed from cart" <br>
"The cart contains:" []