Array.from() function in JavaScript - #59

Veröffentlicht am: 03 Mai 2024
auf dem Kanal: Everyday Be Coding
92
2

#ArrayFrom #JavaScript #ArrayMethods #WebDevelopment #ProgrammingTips #CodingTutorial #SoftwareDevelopment

The from() function in JavaScript is used to create a new array from an array-like or iterable object. Here's an overview of its syntax, descriptions, advantages, and disadvantages:

Syntax:
JavaScript code:
Array.from(arrayLike[, mapFn[, thisArg]])
arrayLike: An array-like or iterable object to convert to an array.
mapFn (Optional): A mapping function to call on each element of the array.
thisArg (Optional): Value to use as this when executing the mapFn.

Descriptions:
Array.from() creates a new shallow-copied array instance from an array-like or iterable object.
It can be used to convert array-like objects (e.g., NodeList, arguments) or iterable objects (e.g., Set, Map) into arrays.
If a mapping function (mapFn) is provided, it will be called on each element of the array-like or iterable object, and the result will be included in the new array.

Advantages:
Converts Iterable Objects: Allows converting iterable objects like Set, Map, NodeList, etc., into arrays easily.
Shallow Copy: Creates a shallow copy of the array, ensuring that changes made to the new array do not affect the original object.
Mapping Function: Offers flexibility by allowing a mapping function to be applied to each element during the conversion process.

Disadvantages:
Performance Overhead: Using a mapping function or dealing with large iterable objects may introduce performance overhead compared to simple array creation methods.
Compatibility: May not be supported in older browsers or environments that do not fully support ECMAScript 6.

Example:
JavaScript code:
// Convert a Set to an Array
const set = new Set([1, 2, 3]);
const array = Array.from(set);
console.log(array); // Output: [1, 2, 3]

// Convert a NodeList to an Array
const nodeList = document.querySelectorAll('p');
const paragraphs = Array.from(nodeList);
console.log(paragraphs); // Array of p elements

// Using mapping function
const squares = Array.from([1, 2, 3], x = x * x);
console.log(squares); // Output: [1, 4, 9]
Array.from() is a powerful utility method in JavaScript, especially useful for working with iterable objects and converting them into arrays while providing flexibility with mapping functions. However, it's essential to be mindful of potential performance implications, especially when dealing with large datasets.

Chapter :
00:00 Introduction to Array.from() Function
00:21 Syntax
00:46 Descriptions
01:11 Advantages
01:37 Disadvantages
01:58 Example
02:24 Summery

Thank you for watching this video
EVERYDAY BE CODING


Auf dieser Seite können Sie das Online-Video Array.from() function in JavaScript - #59 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Everyday Be Coding 03 Mai 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 92 Mal angesehen und es wurde von 2 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!