Array from method can be called with argument items and optional arguments mapfn and thisArg.
Array.from(items, mapfn, thisArg);
The following simple example demonstrate crating new Array using mapfn with multiply logic inside for each items value.
Array.from('123', (item, index) => item * 2); // [2, 4, 6];
What is the third argument? Please give me an example which will showing the case when i should use thisArg.
thisArg is Optional
this when executing mapFn.a = Array.from('2431', function(item){
return item*this.multiply;
}, {multiply:2});
console.log(a)
// will return : [4, 8, 6, 2]
a = Array.from('2431', (item, index) => item * 2);
console.log(a)
// will return : [4, 8, 6, 2]
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from
The third parameter is
Value to use as this when executing mapFn.
const x = Array.from('123', function(item) {
console.log("THIS", this);
return item * 2;
}, {
test: "valueOfThis"
});
console.log("Result:", x);
ES6 version
const that = { test: "ValueOfThis" };
const x = Array.from('123', (item) => {
console.log("THIS", that);
return item * 2;
});
console.log("Result:", x);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With