← #48 Pretty format JSON
#49

Ignore items from array destructuring

Topics: JavaScript

When destructuring an array, you can skip certain items by using blanks:

const dateTime = '2021-02-28T14:57:00';

// Ignore the date part
const [, time] = dateTime.split('T');

// Ignore the seconds
const [hours, minutes, ] = time.split(':');

hours; // '14'
minutes; // '57'

If you are working in a team, then it's a good idea to add comments for skipped items. It also makes the code more readable:

const [
, // date
time
] = dateTime.split('T');

const [hours, minutes, /* seconds */] = time.split(':');

More

Fix a typo or suggest an improvement
#50 Accept any numbers of parameters