[TIL]ES6 array head, tail

Funnystyle·2021년 3월 29일
0

javascript 에서 immutable하게 head, tail 을 만들 수 있는 방법.

var array = [1, 2, 3, 4, 5];
var head = array[0];
var tail = array.slice(1);

// ES6
const array = [1, 2, 3, 4, 5];
const head = ([head]) => head;
const tail = ([, ...tail]) => tail;

참고:
https://stackoverflow.com/questions/35361225/javascript-head-and-tail-on-array-without-mutation#49105244

profile
polyglot

0개의 댓글