Accessing head/tail of Array ES2015
Before in javascript when I wanted to access the head and tail of an array in a functional way I did:
function head(l) {
return l[0];
}
function tail(l) {
return l.slice(1);
}
Now with ES2015 I can just use destructuring and it comes out of the box:
l = [1, 2, 3, 4];
[hd, ...tl] = l;
console.log(hd) // 1
console.log(tl) // [2, 3, 4]
Written by Esteban
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Es2015
Authors
Related Tags
#es2015
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#