T
T
timonck2020-12-31 18:22:49
Python
timonck, 2020-12-31 18:22:49

How to overwrite values ​​in an array of objects?

I have an array of objects

let data = [
    {
        id: 1,
        title: "Product 1",
        images: [
            "http://www.nicolethemes.com/sapphire-html/products/dress1home.jpg",
            "http://www.nicolethemes.com/sapphire-html/products/dress2home.jpg",
            "http://www.nicolethemes.com/sapphire-html/products/dress5home.jpg",
            "http://www.nicolethemes.com/sapphire-html/products/dress6home.jpg",
        ],
        price: 18,
        company: "Bed",
        info:
            "Lorem ipsum .",
        inCart: true,
        count: 3,
        total: 0
    },
    {
        id: 2,
        title: "Product 2",
        images: [
            "http://www.nicolethemes.com/sapphire-html/products/dress3home.jpg",
            "http://www.nicolethemes.com/sapphire-html/products/dress7home.jpg"
        ],
        price: 17,
        company: "Sofa",
        info:
            "Lorem ipsum.",
        inCart: true,
        count: 5,
        total: 0
    }
]

How can I change all inCart to false and count to 0 on click?
I tried like this but it doesn't work
const clear = () => {
        let tempProduct = [...products]
        let res = tempProduct.map(i => {return(
            i.inCart = false,
            i.count = 0)
        });
    };

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
st0ne_c0ld, 2019-03-05
@iiiideb

special_price = div.find('span', attrs={'class' : 'subcategory-product-item__price_special'}).text
AttributeError: 'NoneType' object has no attribute 'text'
What if div.find doesn't find this string in principle (i.e. in the div in which the search takes place, it simply does not exist)?
You can do it like this, for example:

def safe_search(tag, attrs):
    x = div.find('span', attrs={'class' : 'subcategory-product-item__price_special'})
    if x is not None:
        return x.text()
    else:
        return ''

B
Bohdan Petrov, 2020-12-31
@mindyourlifeguide

In react, data is immutable.
Use setState.

M
Mikhail Osher, 2020-12-31
@miraage

products.map((data) => ({
  ...data,
  inCart: false,
  count: 0,
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question