javascriptarraysdictionary

How console log element value during map() in javascript?


I have an array and I create an another array with Array.prototype.map(). How can I console log the x value (the current element being processed in the array) during the map function?

const array1 = [1, 4, 9, 16];
const map1 = array1.map((x) => x * 2);

Solution

  • you can use like this:

    const array1 = [1, 4, 9, 16];
    const map1 = array1.map(function(x){
       console.log(x);
       return x* 2;
    });