-2

Consider an array

a = [-4,58,9,,-91]

So I want to add these element in such way that my result will be b= [-4 ,(58-4 = 54), (54+9=63),(63-91=-28) ] so my array result is [-4,54,63,-28].

Any solution?

1
  • it is not angular ................ Commented Dec 13, 2017 at 5:10

1 Answer 1

2

To modify your array in place, you can use a forEach loop:

const a = [-4,58,9,-91];
a.forEach((elt, i, arr) => {
  if (i > 0) {
    arr[i] = elt + arr[i - 1];
  }
});

console.log(a); // [-4,54,63,-28]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.