0

我终生无法根据每个对象的唯一标识符属性从对象数组中检索单个对象id。下面的代码只返回数组中的第一个对象。我觉得我接近解决方案,但不完全在那里。任何帮助将不胜感激。提前致谢。

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);
}

数据结构如下:

[    
    {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}
]
4

0 回答 0