I'm transforming my array with Array.prototype.map(), and if the current item matches some conditions, I want to change its previous item.
The code below seems not working, how do I do it without a for loop?
var nums = [1, 2, 3, 4, 5];
var res = nums.map((item, index, array) => {
if (item > 3) {
array[index - 1] += 1;
}
return item;
});
console.log(res); // [ 1, 2, 3, 4, 5 ], I want [1, 2, 4, 5, 5]