Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use from method in es6

Shulou Source: shulou.com Published: 2022-05-31 18:12:42 10月03日 Update

Today, the editor will share with you the relevant knowledge points about how to use the from method in es6. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

In es6, the from method is used to convert array-like objects and traverable objects into real arrays, and the result is a new array instance that has been converted, with the syntax "Array.from (pseudo-array objects or iterable objects that you want to convert to an array, callback functions, this objects when callback functions are executed)".

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

How to use from method in es6

This method is used to convert two types of objects into real arrays: array-like objects and traverable objects.

Grammar

Array.from (arrayLike, mapFun, thisArg)

Parameters.

ArrayLike: required. Pseudo-array objects or iterable objects that you want to convert to an array

MapFun: optional. If this parameter is specified, each element in the new array executes the callback function.

ThisArg: optional. The this object when the callback function mapFun is executed.

Return value

A new array instance

Examples are as follows:

Generate an array from String

Array.from ('foo'); / / ["f", "o", "o"]

Generate an array from Set

Const set = new Set (['foo',' bar', 'baz',' foo']); Array.from (set); / / ["foo", "bar", "baz"]

Generate an array from Map

Const map = new Map ([[1, 2], [2, 4], [4, 8]); Array.from (map); / [[1, 2], [2, 4], [4, 8]] const mapper = new Map ([['1','a'], ['2','b']]); Array.from (mapper.values ()); / / ['a','b']; Array.from (mapper.keys ()) / / ['1percent,' 2']

Generate an array from a class array object (arguments)

Function f () {return Array.from (arguments);} f (1, 2, 3); / / [1, 2, 3] is all the content of this article "how to use the from method in es6". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Arrays objects methods functions knowledge articles generation content parameters examples syntax iterations differences large elements most that is tutorials more environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Docker Redmi Apple