标题中几乎所有内容,但我给您举个例子:
// i can't know the content of this string but for the exemple she's here
let str = 'elem-0, elem-1, elem-2, elem-3';
// how to properly write this line because this line is not write in an array 'destructuring way'
let array = str.split(',')[0];
// return me : "elem-0" (and he has to)
console.log(array);
答案 0 :(得分:1)
您可以解构数组元素,例如
let [first, second, ...rest] = str.split(',');
基本上,它按索引工作,第一个变量是arr [0],第二个变量是arr 1,依此类推。 ...rest
将保留数组中未被破坏的其余项目