1// our string
2let string = 'ABCDEFG';
3
4// splits every letter in string into an item in our array
5let newArray = string.split('');
6
7console.log(newArray); // OUTPUTS: [ "A", "B", "C", "D", "E", "F", "G" ]
1const str = 'Hello!';
2
3console.log(Array.from(str)); // ["H", "e", "l", "l", "o", "!"]
1var myString = 'no,u';
2var MyArray = myString.split(',');//splits the text up in chunks
3
1str = 'How are you doing today?';
2console.log(str.split(' '));
3
4>> (5) ["How", "are", "you", "doing", "today?"]
1// Designed by shola for shola
2
3str = 'How are you doing today?';
4console.log(str.split(" "));
5
6//try console.log(str.split("")); with no space in the split function
7//try console.log(str.split(",")); with a comma in the split function
1// créer une instance d'Array à partir de l'objet arguments qui est semblable à un tableau
2function f() {
3 return Array.from(arguments);
4}
5
6f(1, 2, 3);
7// [1, 2, 3]
8
9
10// Ça fonctionne avec tous les objets itérables...
11// Set
12const s = new Set(["toto", "truc", "truc", "bidule"]);
13Array.from(s);
14// ["toto", "truc", "bidule"]
15
16
17// Map
18const m = new Map([[1, 2], [2, 4], [4, 8]]);
19Array.from(m);
20// [[1, 2], [2, 4], [4, 8]]
21
22const mapper = new Map([["1", "a"], ["2", "b"]]);
23Array.from(mapper.values());
24// ["a", "b"]
25
26Array.from(mapper.keys());
27// ["1", "2"]
28
29// String
30Array.from("toto");
31// ["t", "o", "t", "o"]
32
33
34// En utilisant une fonction fléchée pour remplacer map
35// et manipuler des éléments
36Array.from([1, 2, 3], x => x + x);
37// [2, 4, 6]
38
39
40// Pour générer une séquence de nombres
41Array.from({length: 5}, (v, k) => k);
42// [0, 1, 2, 3, 4]
43
44