I can't for the life of me retrieve a single object from an array-of-objects based on each objects unique identifier property id
. The code below only returns the FIRST object in the array. I feel that I'm close to a solution, but not quite there. Any help would be greatly appreciated. Thanks in advance.
removeFromCart(item) {
const cart = this.state.cart;
const element = cart.filter((el, index) => {
let indx = cart[index.id] === index[item.id];
return indx;
});
cart.splice(element, 1);
// updating the cart state
const updateState = ({cart}) => cart;
this.setState(updateState);
}
Data Structure Below:
[
{id: 1, name: "Loaded Nachos", description: "Ground beef, turkey with melted cheddar", price: 10.95},
{id: 2, name: "Bacon Burger Combo", description: "Bacon burger with fries and slaw", price: 11.99},
{id: 3, name: "Spicy Burrito", description: "Bean, sour cream, cheese wrap with ghost peppers", price: 7.75},
{id: 4, name: "Mac N Cheese", description: "Cheddar jack, parm molted in harmony", price: 7.95},
{id: 5, name: "Crab Roll", description: "New England with fries", price: 12.49},
{id: 6, name: "BBQ Special", description: "Meadly of smoked meats (Ribs, Chicken)", price: 16.49}
]