[英]Splitting a word's letters into individual variables with JavaScript
Is there a way to split a word into individual letters with each letter being a different variable? 有没有一种方法可以将一个单词拆分成多个单独的字母,每个字母是一个不同的变量?
For example: 例如:
Hello
Would be
1 = H
2 = e
3 = l
4 = l
5 = o
I'm looking for the simplest way to do this. 我正在寻找最简单的方法来做到这一点。
If you really want each letter in individual objects you can make use of Destructuring assignment 如果您确实希望每个字母都包含在单个对象中,则可以使用“ 分解结构”分配
let [a,b,c,d,e] = "hello" console.log(a, b, c, d, e)
@Hkessel1, @ Hkessel1,
Did you try spread operator in ES6? 您在ES6中尝试过传播算子吗? I am not sure whether this solves your problem? 我不确定这是否可以解决您的问题?
let s= "Hello"; 让s =“ Hello”;
console.log(...s); console.log(... s);
just index through the string. 只是通过字符串索引。
var str = "Hello";
for(var i = 0; i < result.length; i++){
var myVar = str[i]; // get my variable
// do something with it.
}
Just use a variable as below: 只需使用如下变量:
var test="Hello";
Then test[0] means H 然后test [0]表示H
test[1] is e test [1]是e
and so on 等等
you can in es6, do 您可以在ES6中执行
var a,b,c
[a,b,c] = "Hello"
console.log(a,b,c)
but you can't dynamically create the variables, just use an array in that case. 但是您不能动态创建变量,只能在这种情况下使用数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.