Skip to content
Advertisements

Array.from()

Array.from ()” separates the values ​​that are written into. it defines each of the things he separates as a sequence element.

        var x = Array.from("truecodes");
        console.log(x);
        // output: [t,r,u,e,c,o,d,e,s]

The JavaScript Array.from() method allows you to convert Array-like objects (e.g. NodeList and arguments) to an instance of Array.

You can use the Array.from() method to avoid the old hacks used by JavaScript developers to do typical tasks such as:

Get the arguments of a function by converting the arguments object into an instance of Array,
Iterating over a NodeList returned by methods such as querySelectorAll(), etc.


Examples

Example 1

Array.from("truecodes");
// t,r,u,e,c,o,d,e,s

Example 2 (set)

        var arr = new Set(["code","javascirpt","truecodes"])
        console.log(Array.from(arr))
        // ["code", "javascirpt", "truecodes"]

Example 3 (map)

// map:The map method passes the elements one by one to the first argument of the function.

        var arr = new Map([["code","blogger"],["true","codes"]])
        console.log(Array.from(arr))

        // output: 
        // ["code", "blogger"]
        //["true", "codes"]
        // length: 2

Example 4 (for)

        var arr = ["js","css","html","php"]
        for (var i = 0; i < arr.length; i++) {
            var x = Array.from(arr[i]);
            console.log(x)
        }
        
        // output:
        //["j", "s"]
        //["c", "s", "s"]
        //["h", "t", "m", "l"]
        //["p", "h", "p"]

Example 5 (arrow function)

        var x =  Array.from([1, 2, 3], a => a + a);
        console.log(x)
        // output: 2, 4, 6

Example 6 (Sorting in list form)

        var list = (["john","sasha","rick","barry","oliver"])
        console.log(Array.from(list))

        // output:
        // "john"
        //"sasha"
        //"rick"
        //"barry"
        //"oliver"

Example 7

Array.from() Example 7
        let number = Number(prompt("Number to be repeated: "))
        let ret = Number(prompt("How many times to repeat:"))
        let x = Array.from(Array(ret), () => number)
        document.write(x)

Example 8

document.write(Array.from(Array(10), (x, y) => y));

output:

0 , 1 , 2 , 3 , 4 , 5 6 , 7 , 8 , 9


Example 9

let arr = Array.from(Array(10), () => Math.floor(20 * Math.random()));
console.log(arr)

output:

[16, 0, 13, 6, 15, 11, 12, 16, 7, 8]
[0, 19, 4, 8, 17, 10, 5, 1, 16, 7]
[6, 12, 7, 6, 11, 16, 1, 17, 4, 15]
……


Advertisements

2 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: