OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

This function is creating an array for each product added

  • Thread starter Thread starter user22199692
  • Start date Start date
U

user22199692

Guest
Code:
let arrayNomes = new Set()
    let novoArray = []
    const adicionar = () => {
        mercadorias.forEach(mercadoria => {
            if (produto === mercadoria.nome) {
                let total = item + Number(mercadoria.preco)
                setItem(Number(total))
                arrayNomes.add(mercadoria.nome)
            }
        })
        novoArray = [...arrayNomes]
        setNome(novoArray)
    }

I expected it to add a product to the array but without repeating the same item and to show all the items in the array. but it is creating an array for each item added.

<pre><code>let arrayNomes = new Set()
let novoArray = []
const adicionar = () => {
mercadorias.forEach(mercadoria => {
if (produto === mercadoria.nome) {
let total = item + Number(mercadoria.preco)
setItem(Number(total))
arrayNomes.add(mercadoria.nome)
}
})
novoArray = [...arrayNomes]
setNome(novoArray)
}
</code></pre>
<p>I expected it to add a product to the array but without repeating the same item and to show all the items in the array. but it is creating an array for each item added.</p>
 
Top