Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
Use of ternary operator inside filter and map method in javascript?

I have an cart object. I want to change the qty of a product in cart for which i have created a changeQty method inside which I am using filter method to find the respective product and then I am updating the qty.

If I pass other than 0 it works properly. But if I pass 0 as quantity to the changeQty method the product get's removed from the cart.

According to my knowledge filter works based on the return value. But here I am assigning qty to product.qty. And also If I use map instead of filter I am geting 0 in place of that product. So my doubt is what does product.qty = qty returns ?

let cart = [{
    id: 1,
    title: "Product 1",
    qty: 1
  },
  {
    id: 2,
    title: "Product 2",
    qty: 1
  },
]

const changeQty = (pid, qty) => {
  cart = cart.filter(product => product.id === pid ? product.qty = qty : product)
  console.log(cart);
}

changeQty(1, 0)

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Use map instead of filter. You can use spread syntax and overwrite the value of qty for the product that matches the id.

let cart = [{
    id: 1,
    title: "Product 1",
    qty: 1
  },
  {
    id: 2,
    title: "Product 2",
    qty: 1
  },
]

const changeQty = (pid, qty) => {
  cart = cart.map(product => product.id === pid ? { ...product, qty } : product)
  console.log(cart);
}

changeQty(1, 0)

about 4 years ago · Santiago Trujillo Relatório

0

if I pass 0 as quantity to the changeQty method the product get's removed from the cart.

That's because the assignment of 0 to product.qty evaluates to a 0 return value for the filter callback. That is a falsy value, indicating the current cart item should be filtered out. In fact, you don't want to filter at all. On the contrary, you don't want to change the length of the cart array.

If your goal is to mutate the cart structure, then possibly you are looking for find instead of filter:

const cart = [{id:1,title:"Product 1",qty:1},{id:2,title:"Product 2",qty:1}];

const changeQty = (pid, qty) => {
  Object(cart.find(({id}) => id === pid)).qty = qty;
  console.log(cart);
}

changeQty(1, 0);

NB: the call to Object is to deal silently with a product that is not in the cart.

about 4 years ago · Santiago Trujillo Relatório

0

You are modifying the source object which is very bad:

let cart = [{
    id: 1,
    title: "Product 1",
    qty: 1
  },
  {
    id: 2,
    title: "Product 2",
    qty: 1
  },
]

const changeQty = (pid, qty) => {
  cart.forEach(product => product.id === pid ? product.qty = qty : product)
  console.log(cart);
}

changeQty(1, 0)

That's why you are getting unexpected results

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda