2

Consider this array of items:

items:[
 {
  _id: '111',
  quantity: 3
 },
 {
  _id: '222',
  quantity: 7
 }
]

I need to increment the value by 1 by looping using Array#map with CURLY BRACES following the arrow => (look at the code below), but this make the myNewItems null, and I guess this is because the variable item gets lost along the way and ends up with myNewItems to be null

This is how I have iterated in a redux reducer

return {
  ...state,
  myNewItems: items.map((item) => {
    var itemId = '111'

    item._id == itemId ?
      {
        ...item,
        quantity: item.quantity + 1
      } :
      item
  })
}

Now this makes myNewItems to be null instead of it having updated array of items like this below

items:[
 {
  _id: '111',
  quantity: 4 //has been incremented by 1
 },
 {
  _id: '222',
  quantity: 7
 }
]

How do I get through this?

2 Answers 2

3

You are missing the return statement in your map callback.

items.map(
           (item) =>{
              var itemId = '111'
              return item._id == itemId?
              {...item, quantity: item.quantity + 1
              }:item
        })
Sign up to request clarification or add additional context in comments.

Comments

-1

missing return in map higher order function try this

return item._id == itemId
    ?{...item, quantity: item.quantity + 1}
    :item

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.