title: is there a function in lodash to replace matched item
body: <p>I wonder if there is a simpler method in lodash to replace an item in a JavaScript collection? (Possible <a href="https://stackoverflow.com/questions/19860039/replace-in-array-using-lodash">duplicate</a> but I did not understand the answer there:)</p>
  
  <p>I looked at their documentation but could not find anything</p>
  
  <p>My code is:</p>
  
  <pre><code>var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}];
  // Can following code be reduced to something like _.XX(arr, {id:1}, {id:1, name: "New Name"});
  _.each(arr, function(a, idx){
    if(a.id === 1){
      arr[idx] = {id:1, name: "Person New Name"};
      return false;
    }
  });
  
  _.each(arr, function(a){
    document.write(a.name);
  });
  </code></pre>
  
  <p><strong>Update:</strong>
  The object I'm trying to replace with has many properties like</p>
  
  <p>{id: 1, Prop1: ..., Prop2:..., and so on}</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>Thanks to <a href="https://stackoverflow.com/users/949476/dfsq">dfsq</a> but I found a proper solution within lodash that seems to work fine and is pretty neat and I put it in a mixin as well since I've this requirement at many places. <a href="http://jsbin.com/tuwawaluno/1/edit?html,css,js,output" rel="noreferrer">JSBin</a></p>
  
  <pre><code>var update = function(arr, key, newval) {
    var match = _.find(arr, key);
    if(match)
      _.merge(match, newval);
    else
      arr.push(newval);    
  };
  
  _.mixin({ '$update': update });
  
  var arr = [{id: 1, name: "Person 1"}, {id:2, name:"Person 2"}];
  
  _.$update(arr, {id:1}, {id:1, name: "New Val"});
  
  
  document.write(JSON.stringify(arr));
  </code></pre>
  
  <p><strong>Faster Solution</strong>
  As pointed out by @dfsq, following is way faster</p>
  
  <pre><code>var upsert = function (arr, key, newval) {
      var match = _.find(arr, key);
      if(match){
          var index = _.indexOf(arr, _.find(arr, key));
          arr.splice(index, 1, newval);
      } else {
          arr.push(newval);
      }
  };
  </code></pre>