Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!