title: How to add new attribute that need to be calculated recursively in JavaScript
body: <p>I want to dynamically add new attribute to all of the items in an array using map. But while adding it to each item, the value of this attribute is the sum of all previous items newly added attribute </p>
  
  <p>Consider this example : </p>
  
  <pre><code>let persons = [{
  "name": "A",
   "salary": 2
  }, {
  "name": "B",
   "salary": 5
  },{
  "name":"C",
  "salary":12
  }];
  </code></pre>
  
  <p>I want to return :</p>
  
  <pre><code>[{
      "name": "A",
       "salary": 2,
       "sumSalary":2
      }, {
      "name": "B",
       "salary": 5,
       "sumSalary":7
      },{
      "name":"C",
      "salary":12,
      "sumSalary":19
     }];
  </code></pre>
  
  <p>I've tried this one:</p>
  
  <pre><code>let mutatedPersons = persons.map((currentValue, index, mutatedPersons) =&gt; ({
    ...currentValue,
      sumSalary: currentValue.name === 'A' ? currentValue.salary : mutatedPersons[index - 1].sumSalary + currentValue.salary
  }))
  </code></pre>
  
  <p>but i keep getting this : </p>
  
  <pre><code>[
    0: {name: "A", salary: 2, sumSalary: 2}
    1: {name: "B", salary: 5, sumSalary: NaN}
    2: {name: "C", salary: 12, sumSalary: NaN}
  ]
  </code></pre>