title: Add index of multiple arrays together
body: <p>I am trying to add the values of multiple arrays at each index.
  Eg.</p>
  <pre><code>arr1 = [100, 110, 121]
  arr2 = [20, 25, 27.5]
  
  newArr = [120, 135, 148.5]
  
  </code></pre>
  <p>My current approach is below.
  I am finding the percentage change between stocks each day (Day 1, Day 2, Day 3 etc)</p>
  <p>Then I am moving to the next stock and  I want to add the percentage change of stock 1 and stock 2 together. ie Append the new percentage change to the old percentage change.</p>
  <p>What I am trying to do know is check if the index exists as a key in the object, and if it does, add the diff figure to this index.</p>
  <p>If the index doesn't exist I want to just add the diff figure.</p>
  <p>It is working when the index doesn't exist but when the index does exist (ie. The second, third stock etc) the old value for that index is just overwritten with the newer value.</p>
  <p>I want to add them together.</p>
  <p>Is there a simple clean solution for this?</p>
  <p><a href="https://i.stack.imgur.com/1PJSb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1PJSb.png" alt="enter image description here" /></a></p>
  <pre><code>accumPercent = {}
  
  const portfolio = props.test.map((item) =&gt; {
          let oldPrice = item.res.data.o[0]
          item.res.data.o.map((item1, index) =&gt; {
              let diff = ((item.res.data.c[index] - oldPrice) / oldPrice) * 100
              oldPrice = item.res.data.c[index]
  
              if (index in Object.keys(accumPercent)) {
                  accumPercent[index] = accumPercent[index] + diff
              } else {
                  accumPercent[index] = diff
              }
  
          })
      })
  </code></pre>
  <pre><code>    let example = [
          {
              o: [10, 20, 30]
          },
  
  
          {
              o: [10, 40, 60]
          }
      ]
  </code></pre>