0

I want to perform an operation involving the current and the next array element.

For example, add current element with the next:

let arr = [0,1,2,3,4,5];
let newarr = arr.map((a,b) => a+b); //here, a and b are treated as the same element

expecting it to yield a new array of sums of current and next array element:

[0+1, 1+2, 2+3, 3+4, 4+5]

Is it possible to do that with map? If not, is there any other method that is suitable for manipulating multiple array elements in one operation?

2
  • 1
    Do you want it literally this way, or do you need the sums to be evaluated? Commented Mar 19, 2021 at 9:02
  • 1
    Related: Sliding window over Array in JavaScript Commented Mar 19, 2021 at 9:05

5 Answers 5

3

here, a and b are treated as the same element

No. a is the value and b is the index. They happen to be the same in your particular data set.

Is it possible to do that with map?

Not with map itself. That will give you a new value for each value in the array, but you are starting with 6 values and ending up with 5, so you need an additional transformation.

Obviously you also need to use "the next value" instead of "the current index" too.

const arr = [0,1,2,3,4,5];
const newarr = arr.map((value, index, array) => value + array[index + 1]);
newarr.pop(); // Discard the last value (5 + undefined);
console.log(newarr);

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

2 Comments

Oh I see how that works now. Thank you! That's what I was looking for. I guess filtering (i => i) from Mamun's answer is another way to discard values outside array bounds
@Dex it is but not very reliable - it's also going to filter out any zeroes, so [0, 1, 0, 0, 2] would produce [1, 1, 2]
1

You could slice the array and map with the value and value at same index of original array.

const
    array = [0, 1, 2, 3, 4, 5],
    result = array.slice(1).map((v, i) => array[i] + v);

console.log(result);

Comments

1

The second parameter in map is the index. Since map returns results for each iteration you can filter the unwanted item from the new array:

let arr = [0,1,2,3,4,5];
let newarr = arr.map((a,b) => a+arr[b+1]).filter(i => !isNaN(i));
console.log(newarr);

2 Comments

Does the filter in this example, ensure that all elements are in range of array length?
@Dex, your input array and result array is not of same length. Since the solution here uses index+1 there is an item NaN in the result, filter just removes that in this solution:)
0

You can use reduce for that:

const arr = [0, 1, 2, 3, 4, 5];

const out = arr.reduce((acc, el, i) => {
  if (i === arr.length - 1) { // don't do anything for the last element
    return acc;
  }
  acc.push(el + arr[i + 1]);
  return acc;
}, []);

console.log(out)

Comments

0

Using Array.prototype.slice(), Array.prototype.forEach().

const data = [0, 1, 2, 3, 4, 5],
  numbers = data.slice(0, -1),
  result = [];

numbers.forEach((number, index) => {
  result.push(number + data[index + 1]);
});

console.log(result);

2 Comments

Why use .forEach() over .map() here?
I just tried different way to solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.