Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use from method in es6

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report