You can loop through your array to access each of the elements and divide it by whatever number you need. I'm not returning an array or updating the elements of the array because I imagine you're going to use them separately.
var promedio = [220,320];
var cantidad = 4;
for (i = 0; i < promedio.length; i++) {
var resultado = promedio[i] / cantidad;
console.log(resultado);
}
You have an answer with a loop and another with map, I'm going to give you a third one using forEach. With forEachyou go through all the elements of an array, then you only have to update the value:
var promedio = [220, 320];
var resultado = [];
var divisor = 4;
promedio.forEach(function(elemento, indice) {
resultado[indice] = elemento / divisor;
});
console.log(resultado);
A simple example with array.map , will generate a new array based on the one you are going to loop through
You can loop through your array to access each of the elements and divide it by whatever number you need. I'm not returning an array or updating the elements of the array because I imagine you're going to use them separately.
You have an answer with a loop and another with
map
, I'm going to give you a third one usingforEach
. WithforEach
you go through all the elements of an array, then you only have to update the value: