Первый
Использование метода Array.from ()
let hello = "World" // first Array.from(hello).map(i => console.log(i))
второй
Использование для … петли
// second for (let char of hello){ console.log(char) }
в третьих
Использование встроенного метода split () в string ()
// third hello.split("").forEach(i => console.log(i))
четвертый
Древний для петли
// fourth for (let i = 0; i < hello.length ; i++) { console.log(hello[i]) }
// пять
Использование функции причудливого генератора и для … петля
// five advance function* iter(str) { let i = 0 while(i < str.length) { yield str[i]; i++ } } for (let char of iter(hello)){ console.log(char) }
Дайте мне знать других. Спасибо
Оригинал: “https://dev.to/nahidmbstu/five-cool-ways-to-iterate-over-javascript-string-3kdl”