2017-10-16 4 views
0

인덱스 0 이상의 배열 값에만 관심이있을 때 배열 소멸시 쓸모없는 변수를 선언하지 않도록 할 수 있습니까?배열 소멸에서 반환 된 값을 어떻게 무시할 수 있습니까?

다음에서 나는 a을 선언하는 것을 피하고 싶습니다. 색인 1에만 관심이 있습니다.

// How can I avoid declaring "a"? 
 
const [a, b, ...rest] = [1, 2, 3, 4, 5]; 
 

 
console.log(a, b, rest);

+1

관련 : [Destructuring 배열의 두 번째 값을 얻을?] (https://stackoverflow.com/q/44559964/218196), [긴 배열 개체 destructuring 솔루션?] (https : //로 stackoverflow.com/q/33397430/218196) –

답변

2

Can I avoid declaring a useless variable when array destructuring when I am only interested in array values beyond index 0?

당신이 빈 과제의 첫 번째 인덱스를 떠날 경우 예, 아무것도 할당되지 않습니다. 이 동작은 explained here입니다.

// The first value in array will not be assigned 
 
const [, b, ...rest] = [1, 2, 3, 4, 5]; 
 

 
console.log(b, rest);

당신이 원하는 어디든지 당신이 나머지 요소 다음을 제외하고, 원하는만큼 쉼표를 사용할 수 있습니다

const [, , three] = [1, 2, 3, 4, 5]; 
 
console.log(three); 
 

 
const [, two, , four] = [1, 2, 3, 4, 5]; 
 
console.log(two, four);

이 다음은 오류가 발생합니다 :

const [, ...rest,] = [1, 2, 3, 4, 5]; 
 
console.log(rest);

관련 문제