ES6 One Liners to Show Off

Follow us on LinkedIn for our latest data and tips!

, ,

ES6 One Liners to Show Off

This is a guest post by Hemanth H.M and was originally published on H3manth.com.

Here is a collection of ES6 one liners to show off!

NodeList to Array:

var headings = [ ... document.querySelectorAll('h1') ];Unique Arrays:
[ ...new Set(array) ] 

Destructuring:

var {foo, bar} = {foo: "lorem", bar: "ipsum"};
// foo => lorem and bar => ipsum

Swap like a snake:

[a,b] = [b,a]

Max in array?:

Math.max(...array);

List comprehension:

[ for (value of ["Harriet", "178"]) value ].join(" was ");
// Would give us "Harriet was 178"
// BTW it was Charles Darwin's tortoise.

_map?

[{id: 1}, {id: 2}].map(x => x.id)
// [1,2]

Object.isz!

let Object.isz = (x, y) => x === y || Object.is(x, y);
//^ Credits to Mark S. Miller.

Is it a hidden file?:

isHidden = (file) => /^\.|~$/.test(file);
isHidden(".DS_STORE") // true

Repeat with me:

(0/0+"").repeat("7")+ " BatMan!"
// "NaNNaNNaNNaNNaNNaNNaN BatMan!"

If you are using some, please do let me know!

Hope it blew your mind! ;)

blew