title: replace or push object in array using underscore
body: <p>I have one object with new data coming in and one array  with data that has been saved. If cnName from newData already exists in savedData in the cnData array then that object in savedData should be replaced with newData.cnData[0] object. If it does not exists then the data should to pushed to the cnData array in savedData</p>
  
  <pre><code>newData = {
    "cnGroupName": "cnGroupName1",
    "cnData": [{
      "cnName": "cn3",
      "data": {
         "color": "blue",
         "size": "42",
         "position": "right"
      }
    }]
  }
  
  savedData = [{
    "cnGroupName": "cnGroupName1",
    "cnData": [{
      "cnName": "cn1",
      "data": {
        "color": "red",
        "size": "42",
        "position": "right"
      }
    }, {
      "cnName": "cn2",
      "data": {
        "color": "blue",
        "size": "11",
        "position": "top"
      }
    }]
  }]
  </code></pre>
  
  <p>I am using underscore</p>
  
  <pre><code>if(savedData.length){
      _.each(savedData, function(num, i){
  
          if(savedData[i].cnGroupName == newData.cnGroupName ){
  
              _.each(savedData[i].cnData, function(num, x){
  
  
                 if(savedData[i].cnData[x].cnName == newData.cnData[0].cnName){
  
                      // the cnName already exists in savedData so replace
                      savedData[i].cnData[x] = newData.cnData[0] 
  
  
                 }else{ 
                      // the cnName  does NOT exist in savedData so push the new object to savedData
                     savedData[i].cnData.push(newData.cnData[0])
                  }
              })
          }else{
              // the cnGroupName from newData is not in saveData so push newData
              savedData.push(newData)
          }
      })
  }
  </code></pre>
  
  <p>the reason this does not work is because in the second _.each() statment. If the cnName does NOT exist in savedData it will push newData once for every object that is already in savedData. So if there are already 2 saved objects in savedData then the newData will be pushed 2 times</p>
  
  <p>you can see the console logs in this plunker that the newData is being pushed twice</p>
  
  <p><a href="http://plnkr.co/edit/GkBoe0F65BCEYiFuig57?p=preview" rel="nofollow">http://plnkr.co/edit/GkBoe0F65BCEYiFuig57?p=preview</a></p>
  
  <p>My 2 questions are </p>
  
  <ol>
  <li>is there a cleaner way of doing this in underscore?</li>
  <li>is there a way to stop _.each() from continuing the loop ?</li>
  </ol>