How to return values using .map () in JavaScript

0

I'm trying to make a .map() method, but I'm not getting it. My last code was this:

var double = x => x * 2;

function filtro(funcao, numeros) {
	let arr = [];
  
  for (let i = 0; i < numeros.length; i++){
  	arr.push(numeros[i](funcao));
   }
   return arr;
}
  		


console.log(filtro(double, [1, 2, 3, 4, 5, 6, 7, 8, 10]));

I would have to return: 2,4,6,4,10 .. etc ..

Can anyone help me?

    
asked by anonymous 27.01.2018 / 00:11

1 answer

1

The way the code is, you simply call the method passing as a numeros[i] parameter, see that i is the index of each element.

var numeros = [1, 2, 3, 4, 5, 6, 7, 8, 10];
var i = 4; // Índice

// Saida => 5
console.log(numeros[i]);

i = 2; // Índice

// Saida => 3
console.log(numeros[i]);


i = 6; // Índice

// Saida => 7
console.log(numeros[i]);

See working

var double = (x) => x * 2;

function filtro(funcao, numeros) {
  let arr = [];
  for (let i = 0; i < numeros.length; i++){
    arr.push(funcao(numeros[i]));
  }
  return arr;
}

console.log( filtro(double, [1, 2, 3, 4, 5, 6, 7, 8, 10]) );
    
27.01.2018 / 00:28