DEV Community

avinash-repo
avinash-repo

Posted on

Js _underscore Trick

Certainly:

https://onecompiler.com/javascript/423ppjau6


console.log([...Array(10)],Array(10))

[undefined, undefined,  undefined, undefined,undefined,undefined,  undefined, undefined,  undefined, undefined
] [ <10 empty items> ]

const newArr=[...Array(10)].map((element, index) => index + 1)
console.log(newArr)
output 1,2,3,4,5,6,7,8,9,10

Enter fullscreen mode Exit fullscreen mode

In this version, both parameters, element and index, are named explicitly. However, since the element is not used in the mapping function, it could be replaced with any other valid variable name, such as _. For example:

[...Array(10)].map((_, index) => index + 1)
Enter fullscreen mode Exit fullscreen mode

Both versions would work identically, producing an array from 1 to 10.

The provided code consists of JavaScript operations to create arrays and perform mapping operations on them. Let's break down each section of the code:

  1. Initialization of myA array:
const myA = [...Array(Math.ceil((30 - 3 + 1) / 3)).keys()];
Enter fullscreen mode Exit fullscreen mode

This line creates an array myA containing values from 0 to Math.ceil((30 - 3 + 1) / 3) - 1, which translates to Math.ceil(28 / 3), resulting in an array [0, 1, 2, ..., 9].

  1. Logging arrays:
console.log(myA, "dd", [...Array(10)].map((_, index) => index + 1));
Enter fullscreen mode Exit fullscreen mode

This line logs the array myA and another array containing numbers from 1 to 10.

  1. Creating arrayFromThreeToThirty:
const arrayFromThreeToThirty = [ 1, 2, 3, 4,  5, 6, 7, 8, 9,10].map(x => x * 3);
Enter fullscreen mode Exit fullscreen mode

This line manually creates an array containing numbers from 1 to 10 and then multiplies each element by 3, resulting in an array [3, 6, 9, 12, 15, 18, 21, 24, 27, 30].

  1. Creating arrayFromThreeToThirty1:
const arrayFromThreeToThirty1 = [...Array(10)].map((_vnull, key) => key + 1).map(x => x * 3);
Enter fullscreen mode Exit fullscreen mode

This line first creates an array [undefined, undefined, ..., undefined] with a length of 10 using spread syntax, then maps each index with its corresponding value plus one, and finally multiplies each resulting value by 3, resulting in the same array as arrayFromThreeToThirty.

  1. Squaring numbers:
const numbers = [1, 2, 3, 4, 5];
const sq = numbers.map((value, _keynull) => value * value);
Enter fullscreen mode Exit fullscreen mode

This code squares each number in the numbers array, resulting in an array [1, 4, 9, 16, 25].

  1. Squaring numbers without using _underscore:
const numbers1 = [1, 2, 3, 4, 5];
const sq1 = numbers1.map(value => value * value);
Enter fullscreen mode Exit fullscreen mode

This is similar to the previous operation but without the use of _underscore, producing the same result.

  1. Mapping an array:
[...Array(10)].map((element, index) => index + 1)
Enter fullscreen mode Exit fullscreen mode

This line creates an array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] by mapping each index with its corresponding value plus one. However, since this array is not assigned to any variable or logged, it doesn't have any observable effect.

Overall, the code demonstrates the creation of arrays with specific patterns and the use of the map function to manipulate array elements.

Without _Underscore Certainly:

const numbers = [1, 2, 3, 4, 5];
const sq = numbers.map(value => value * value);
Enter fullscreen mode Exit fullscreen mode

In this version, the _ placeholder is omitted entirely. It's not needed if you're not using the index parameter in the mapping function. The function simply takes value as the parameter representing each element of the numbers array.

Certainly! You can create an array containing values from 1 to 20 using various methods in JavaScript. One way is to use the Array.from() method or the spread operator (...) with Array.prototype.keys() method. Here's how you can do it:

Using Array.from() method:

const arrayFromOneToTwenty = Array.from({ length: 20 }, (_, index) => index + 1);
console.log(arrayFromOneToTwenty);
Enter fullscreen mode Exit fullscreen mode

Using spread operator and Array.prototype.keys():

const arrayFromOneToTwenty = [...Array(20).keys()].map(x => x + 1);
console.log(arrayFromOneToTwenty);
Enter fullscreen mode Exit fullscreen mode

Both methods will create an array with values from 1 to 20. You can choose the one that suits your preference or coding style better.

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

AI generated/assisted posts - like this one - should adhere to the guidelines for such content.