Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

ES6 One Liners to Show Off

| Comments

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

NodeList to Array:

1
var headings = [ ... document.querySelectorAll('h1') ];

Unique Arrays:

1
[ ...new Set(array) ]

Destructuring:

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

Swap like a snake:

1
[a,b] = [b,a]

Max in array?:

1
Math.max(...array);

List comprehension:

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

_map?

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

Object.isz!

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

Is it a hidden file?:

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

Repeat with me:

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

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

Hope it did blew your mind! ;)

blew

Comments