2

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);
5
  • Use {} to create a block, instead of doing an implicit return => Commented Jun 19, 2021 at 10:00
  • Don't use arrow functions if you don't know how they work: MDN: Arrow function expressions Commented Jun 19, 2021 at 10:00
  • 1
    @PsyGik "Use {} to create a closure" - That's a block and not a "closure" Commented Jun 19, 2021 at 10:01
  • I get -1 but my qestion but have legitimate. My question is very clear. For my aspect have resarch and effort, and answer for me was super useful. Thank you again. Commented Jun 26, 2021 at 19:15
  • Yes, thank you again. Somebody else also give me +1 and other senior also answer for me but ... Commented Jun 27, 2021 at 11:32

5 Answers 5

3

You can use console.log in a map like this:

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

You are using a shorthand return:

() => x returns x just as () => { return x; }.

But since you are not only returning something you can not use the concise arrow function syntax.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you everebody! This code missing an ")" character
1

you can use like this:

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

Comments

1

You can print it like below.

array1.map(function(x) {
      console.log(x*2);
      return x * 2
});   

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

Comments

1

You can use an array:

const array1 = [1, 4, 9, 16];

console.log('Mapping array1...');
const map1 = array1.map((x) => [x * 2, console.log(x)][0]);
console.log('Maped successfully, map1 =', map1);

2 Comments

Thank you. It's work fine but I don't understand the syntax. How can I find description about this? I yet searched on MDN.
@MilánNikolics It would be better to use normal block functions instead of arrow functions, since you can have multiple commands and stuff. But if you need to use arrow functions, this may be an option. It creates an array with two elements: x * 2, which is what you want to return, and a console.log statement, which logs x and returns undefined. Then, the arrow function returns the first element of the array with [0], which contains x * 2, ignoring the undefined returned by the console.log.
1

Use {} to create a block

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.