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?

Dex
  • 153
  • 1
  • 14

5 Answers5

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);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 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 Mar 19 '21 at 09:12
  • 1
    @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]` – VLAZ Mar 19 '21 at 09:14
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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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);
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • Does the filter in this example, ensure that all elements are in range of array length? – Dex Mar 19 '21 at 09:10
  • 1
    @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:) – Mamun Mar 19 '21 at 09:14
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)
Ramesh Reddy
  • 10,159
  • 3
  • 17
  • 32
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);