title: React Reducer function to return array of objects
body: <p>If I have an array of objects representing say a shopping cart (in React)</p>
  
  <pre><code>[ { id: 1, qty: 1 } ]
  </code></pre>
  
  <p>what would be a neat way of adding an item to this <code>state</code> ?</p>
  
  <p>I would need to check if the array contains an object with that id, if it did then update it's quantity, if not then just create a new array element adding this new object.</p>
  
  <p>I am getting myself in a mess trying to write this with new ES6 syntax in a functional style ... this is what I have so far ...</p>
  
  <pre><code>let initialState = []
  
    export default function shoppingCart(state=initialState, action) {
      switch(action.type) {
          case 'ADD_TO_CART':
              // check the array of objects and only act on the one we are interested in 
              let newQty = action.payload.qty
              console.log("state : ", state)
              for(let i = 0; i &lt; state.length; i++) {
                 if (state[i].id === action.payload.id) {
                     newQty = state[i].qty + action.payload.qty
                     console.log("QTY : " + newQty)
                 }
             }
             //Obviously this will not work if the {id ...} already exists
             return [ ... state, {id:action.payload.id, qty:newQty} ] // todo: update this to contain the payload with qty included
  
         case 'REMOVE_FROM_CART':
             return state.filter(elem =&gt; elem.id !== action.payload.id)
         default:
             return state
     }
  }
  </code></pre>