Sai
Sai

Reputation: 275

What does map(Number) do here?

var strArr = ["[1,2,3,4]","[1,2,3,4"]];
var arr1 = strArr[0].match(/\d+/g).map(Number); 

I know that the map() method creates a new array with the results of calling a provided function on every element in the calling array. Here as Number is a wrapper object I am not able to understand what's going on.

What I understood is that if I console log removing the map method I get an array of strings whereas including the map method I get an array of numbers.I would like to know how map is able to take each string and converting to number.

Upvotes: 17

Views: 54267

Answers (4)

Roman Karagodin
Roman Karagodin

Reputation: 859

Actually Number is a function, and it can easily be proved:

typeof Number === 'function'; // true

Hence it can be used in any case where functions are expected (and naturally it can be passed as an argument):

Number("5") === 5; // true
let arr = ["442", "452h", "424", "foo", "bar", "31", "35"];
arr = arr.filter(x => !isNaN(Number(x))); // ["442", "424", "31", "35"]

And finally:

const arr2 = arr.map(x => Number(x)); // [442, 424, 31, 35]
const arr3 = arr.map(Number); // [442, 424, 31, 35] – the same result

UPD: To make it look extra-easy, let us compose a simple function that does exactly the same thing as the inbuilt Number function (converts values of other types to the number type):

const toNumber = x => +x;
// OR: function toNumber(x) { return +x; }

const arr4 = arr.map(toNumber); // also [442, 424, 31, 35]

Upvotes: 7

gbtimmon
gbtimmon

Reputation: 4322

strArr[0] //selects the first string. "[1,2,3,4]"

.match(/\d+/g)  // give an array of all continuos strings of digits. ['1','2','3','4']

.map(Number)  // Calls Number on each value in the array (casting it to a number)
              // and returns the array of results. [1,2,3,4]
              //
              // Keep in mind Number(val) attempts to create a Number from 
              // what ever is passed to it. If it is a string itll
              // try to convert the string to a numerical value. 

Its a complicated way of parsing a string containing an array literal. Seems like a complicated way of doing:

JSON.parse(strArr[0]) 

but without more context I cant tell you if its bad programming or if there is a good reason for it.

Upvotes: 5

Patrick Roberts
Patrick Roberts

Reputation: 51876

var arr1 = strArr[0].match(/\d+/g).map(Number);

is equivalent to

var arr1 = strArr[0].match(/\d+/g).map((str, ind, arr) => Number(str, ind, arr));

The reason Number works despite passing extra arguments in is because it ignores everything but the first argument. You cannot expect every function to behave accordingly, since not all functions ignore everything but the first argument, but in this case it is a convenient shortcut. Another neat example that works well is converting truthy and falsy values to booleans:

var arrBool = arrAny.map(Boolean);

Upvotes: 20

Jimenemex
Jimenemex

Reputation: 3166

You are passing in the Number object/function and converting a single thing that matches that regex in strArr into a Number

It's the same as doing Number(x), where x is an index in the array.

For example,

var num1 = "1";
var num2 = "2";
var result = Number(num1) + Number(num2); // This will work since they are both numbers now.

So calling map on the strings in the array just converts them that specific index into a Number object.

Learn more about Numbers here.

Upvotes: 2

Related Questions