Maybe trivial but what is an elegant way of dividing elements in one array by another (assume arrays are of equal length)? For instance
var A = [2,6,12,18]
var B = [2,3,4,6]
Dividing should give me: [1,2,3,3]
If you have ES5 support, this may be a good option:
var result = A.map(function(n, i) { return n / B[i]; });
Where n
in callback represents the iterated number in A
and i
is the index of n
in A
.
Assuming the two arrays are always the same length:
var C = [];
for (var i = 0; i < A.length; i++) {
C.push(A[i] / B[i]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With