If i have an array A = [1, 4, 3, 2]
and B = [0, 2, 1, 2]
I want to return a new array (A - B) with values [1, 2, 2, 0]
. What is the most e
Use map method The map method takes three parameters in it's callback function like below
currentValue, index, array
var a = [1, 4, 3, 2],
b = [0, 2, 1, 2]
var x = a.map(function(item, index) {
// In this case item correspond to currentValue of array a,
// using index to get value from array b
return item - b[index];
})
console.log(x);