A new method of converting arrays into JavaScript array.from

Source: Internet
Author: User
Tags javascript array

In the past, we used a variety of methods to convert things like array objects (such as arguments and NodeList ) into true values. For example, the following:

    • Convert NodeList to an array

Basically, all of these things we can use Array.prototype.slice.call() in such a way will arguments and NodeList so turn into the desired form.

Now, we have a more straightforward way to directly convert the objects of these class arrays into real objects.

Will NodeListTransformed into Array
var divs = Array.from (Document.queryselectorall (' div ')); // array[232] (every DIV on the page)

Will argumentsTransformed into Array
function something () {  var args = array.from (arguments);   // array[' yes ', 1, {}] }something (' yes ', 1, {});

Will StringTransformed into Array
// very much like ' JavaScript '. Split (') // ["J", "a", "V", "a", "S", "C", "R", "I", "P", "T"]

These formulations are very simple and clear, no need to use slice indirect methods such as!

Original address: http://it.lindukj.cn/archives/1405

A new method of converting arrays into JavaScript array.from

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.