title: Unable to update an array of objects with another array of objects
body: <p>I have two different arrays of objects, <code>storedArray</code> is stored in my file system and <code>inputArray</code> is made from user's input to update <code>storedArray</code>. The minimum length of each of the array is 1 and there's no cap on the maximum number. Also they doesn't necessarily have to be of the same length. So what I want is to loop over each array and:</p>
  <ol>
  <li>if name from <code>inputArray</code> matches with the <code>storedArray</code> name and age is same, then do nothing in the <code>storedArray</code> but keep that object in <code>storedArray</code>. (example John).</li>
  <li>if name from <code>inputArray</code> matches with the <code>storedArray</code> name and age is not same, then update only age value in the old object in <code>storedArray</code>. (example Jane).</li>
  <li>if there is a new object inside <code>inputArray</code> with a different name that doesn't match with any in <code>storedArray</code> name, then push the new object to <code>storedArray</code>. (example Morris).</li>
  <li>Must remove other objects in <code>storedArray</code> which does not match with <code>inputArray</code>. (example Joanna, Jim).</li>
  </ol>
  <p>Update this</p>
  <pre><code>const storedArray = [
          {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25, &quot;courses&quot;: 5},
          {&quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 21, &quot;courses&quot;: 3},
          {&quot;name&quot;: &quot;Joanna&quot;, &quot;age&quot;: 19, &quot;courses&quot;: 2},
          {&quot;name&quot;: &quot;Jim&quot;, &quot;age&quot;: 20, &quot;courses&quot;: 4},
  ];
  </code></pre>
  <p>from this</p>
  <pre><code>const inputArray = [
          {&quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 23, &quot;courses&quot;: 0},
          {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25, &quot;courses&quot;: 0},
          {&quot;name&quot;: &quot;Morris&quot;, &quot;age&quot;: 18, &quot;courses&quot;: 0}
  ];
  </code></pre>
  <p>to this:</p>
  <pre><code>const storedArray = [
          {&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 25, &quot;courses&quot;: 5},
          {&quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 23, &quot;courses&quot;: 3},
          {&quot;name&quot;: &quot;Morris&quot;, &quot;age&quot;: 18, &quot;courses&quot;: 0}
  ];
  </code></pre>
  <p>I tried this with <code>for of</code> loop but I get 22 results. And some of them are missing. Moreover, I tried it by pushing it into a new array. There are other posts here in SO with the same titles but the end goal doesn't match with my one. But still I tried their code but doesn't work.</p>
  <p>Here's what I tried:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const storedArray = [
          {"name": "John", "age": 25, "courses": 5},
          {"name": "Jane", "age": 21, "courses": 3},
          {"name": "Joanna", "age": 19, "courses": 2},
          {"name": "Jim", "age": 20, "courses": 4}
  ];
  
  const inputArray = [
          {"name": "Jane", "age": 23, "courses": 0},
          {"name": "John", "age": 25, "courses": 0},
          {"name": "Morris", "age": 18, "courses": 0}
  ];
  
  let newArray = [];
  
  for(let item of storedArray) {
      for(let each of inputArray) {
          if(item.name === each.name &amp;&amp; item.age === each.age){
              newArray.push(item);
          }else if(item.name === each.name &amp;&amp; item.age !== each.age) {
              item.age = each.age;
              newArray.push(item);
          }else if(item.name !== each.name){
              newArray.push(each);
              newArray.push(item);
          }
      }
  }
  
  console.log(newArray);</code></pre>
  </div>
  </div>
  </p>