Q:

convert string to array js

str = 'How are you doing today?';
console.log(str.split(' '));

>> (5) ["How", "are", "you", "doing", "today?"]
3
const str = 'Hello!';

console.log(Array.from(str)); //  ["H", "e", "l", "l", "o", "!"]
9
const str = 'Hello!';

const arr = Array.from(str);
//[ 'H', 'e', 'l', 'l', 'o', '!' ]
1
var myString = 'no,u';
var MyArray = myString.split(',');//splits the text up in chunks
11
var fruits = 'apple, orange, pear, banana, raspberry, peach';
var ar = fruits.split(', '); // split string on comma space
console.log( ar );
// [ "apple", "orange", "pear", "banana", "raspberry", "peach" ]
3
// If you want to split on a specific character in a string:
const stringToSplit = '01-02-2020';
console.log(stringToSplit.split('-')); 
// ["01", "02", "2020"]

// If you want to split every character:
const stringToSplit = '01-02-2020';
console.log(Array.from(stringToSplit));
// ["0", "1", "-", "0", "2", "-", "2", "0", "2", "0"]
1
let myArray = str.split(" ");
0
// our string
let string = 'ABCDEFG';

// splits every letter in string into an item in our array
let newArray = string.split('');

console.log(newArray); // OUTPUTS: [ "A", "B", "C", "D", "E", "F", "G" ]
0
a=anyElement.all

// console.log(a)
Array.from(a).forEach(function (element){
    console.log(element)
})
1
// créer une instance d'Array à partir de l'objet arguments qui est semblable à un tableau
function f() {
  return Array.from(arguments);
}

f(1, 2, 3); 
// [1, 2, 3]


// Ça fonctionne avec tous les objets itérables...
// Set
const s = new Set(["toto", "truc", "truc", "bidule"]);
Array.from(s);   
// ["toto", "truc", "bidule"]


// Map
const m = new Map([[1, 2], [2, 4], [4, 8]]);
Array.from(m);                          
// [[1, 2], [2, 4], [4, 8]]  

const mapper = new Map([["1", "a"], ["2", "b"]]);
Array.from(mapper.values());
// ["a", "b"] 

Array.from(mapper.keys());
// ["1", "2"]

// String
Array.from("toto");                      
// ["t", "o", "t", "o"]


// En utilisant une fonction fléchée pour remplacer map
// et manipuler des éléments
Array.from([1, 2, 3], x => x + x);      
// [2, 4, 6]


// Pour générer une séquence de nombres
Array.from({length: 5}, (v, k) => k);    
// [0, 1, 2, 3, 4]

-1

New to Communities?

Join the community