从具有数组解构的数组中获取元素的正确方法是什么?
What is the proper way to get an element from an array with array destructuring?
几乎所有内容都在标题中,但我给你举个例子:
// 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);
你可以像这样解构数组元素
let [first, second, ...rest] = str.split(',');
基本上它是按索引工作的,第一个变量是arr[0],第二个是arr1,依此类推。 ...rest
将保留数组中未被破坏的剩余项目
几乎所有内容都在标题中,但我给你举个例子:
// 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);
你可以像这样解构数组元素
let [first, second, ...rest] = str.split(',');
基本上它是按索引工作的,第一个变量是arr[0],第二个是arr1,依此类推。 ...rest
将保留数组中未被破坏的剩余项目