title: Update if exists or add new element to array of objects - elegant way in javascript + lodash
body: <p>So I have an array of objects like that: </p>
  
  <pre><code>var arr = [
    {uid: 1, name: "bla", description: "cucu"},
    {uid: 2, name: "smth else", description: "cucarecu"},
  ]
  </code></pre>
  
  <p><code>uid</code> is unique id of the object in this array. I'm searching for the elegant way to modify the object if we have the object with the given <code>uid,</code> or add a new element, if the presented <code>uid</code> doesn't exist in the array. I imagine the function to be behave like that in js console: </p>
  
  <pre><code>&gt; addOrReplace(arr, {uid: 1, name: 'changed name', description: "changed description"})
  &gt; arr
  [
    {uid: 1, name: "bla", description: "cucu"},
    {uid: 2, name: "smth else", description: "cucarecu"},
  ]
  &gt; addOrReplace(arr, {uid: 3, name: 'new element name name', description: "cocoroco"})
  &gt; arr
  [
    {uid: 1, name: "bla", description: "cucu"},
    {uid: 2, name: "smth else", description: "cucarecu"},
    {uid: 3, name: 'new element name name', description: "cocoroco"}
  ]
  </code></pre>
  
  <p>My current way doesn't seem to be very elegant and functional: </p>
  
  <pre><code>function addOrReplace (arr, object) {
    var index = _.findIndex(arr, {'uid' : object.uid});
    if (-1 === index) {
      arr.push(object);
    } else {
      arr[index] = object;
    }
  } 
  </code></pre>
  
  <p>I'm using lodash, so I was thinking of something like modified <code>_.union</code> with custom equality check. </p>