There is an array:
let arr=[
[1000,800,1,"true"],
[1500,0,2,"false"],
[1600,0,3,"true"],
[2500,300,4,"false"]
]
I want the result:
let arr_result=[
[1000,800,1,"true"],
[500,0,2,"false"],
[100,0,3,"true"],
[900,300,4,"false"]
]
That is, let the latter sub-array element[0] subtract the previous sub-array element[0].
I need to do it in javascript.
How to do it?
>Solution :
You can use map method and then simply use index param to get previous element by using array[index - 1] and then first element of that sub array.
let arr = [
[1000, 800, 1, "true"],
[1500, 0, 2, "false"],
[1600, 0, 3, "true"],
[2500, 300, 4, "false"]
]
const result = arr.map(([e, ...rest], i) => (
[i ? e - arr[i - 1][0] : e, ...rest]
))
console.log(result)