Cool ES2019 features this year!

This year es6 is releasing some new features! Some people like to live years in the future. But most of us want to only start adopting the most important things here and now.

Here’s some of the features that you can start using. Much of this is taken from the article published by Faraz Kelhini here. Also you can check out the actual white-paper here.

Object.fromEntries()
trimStart()
trimEnd()
flat()
flatMap()

What do each of these do for us.

Object.fromEntries()

basically Object.fromEntries() is the opposite of Object.entries(). Object.entries() if you didn’t know converts an object into a list of tuples. So Object.fromEntries() converts a list of tuples back into an object.

For example:

Object.entries()

const obj = {foo: 1, bar: 2, zee: 3};

console.log(Object.entries(obj));
// [["foo", 1], ["bar", 2], ["zee", 3]]

Object.fromEntries()

const arrOfTuples = [["foo", 1], ["bar", 2], ["zee", 3]];console.log(Object.fromEntries(arrOfTuples))
// {foo: 1, bar: 2, zee: 3}

In most cases you probably dont need to convert between the two. But one of the coolest ways you can use this is in url params.

For example:

const paramsString = 'param1=cat&param2=dog';
const searchParams = new URLSearchParams(paramsString);

Object.fromEntries(searchParams);
// {param1: "cat", param2: "dog"}

URL params are probably the best use case I can think of for this.

.trimStart() and trimEnd()

These string functions remove padding from the front or the end of a string. In other words removing white space.

const newString = '     foo     ';console.log(newString.trimStart()) // 'foo     '
console.log(newString.trimEnd()) // ' foo'

.flat() and flatMap()

EcmaScript is slowly removing the need to use third party libraries such as lodash. .flat() is used to flatten out an array .flatMap is the combination of .map() and .flat(). Preforming both on the array.

const newArr = [1,43, 56, 76, [34,54, 65]];console.log(newArr.flat()) // [1,43, 56, 76, 34,54, 65];

Flat can also accept a value of the levels deep to flatten. The default value is 1.

const arr = [56, [45, [22] ] ];console.log(arr.flat()) // [56, 45, [22] ]
console.log(arr.flat(1)) // [56, 45, [22] ]
console.log(arr.flat(2)) // [56, 45, 22 ]

Here’s an example of flatMap()

const arrWords = ['cat', 'car', 'clap' ];console.log(arrWords.map(word => word.split('')); // [["c", "a", "t"],["c", "a", "r"], ["c", "l", "a", "p"]]console.log(arrWords.flatMap(word => word.split('')) // ["c", "a", "t","c", "a", "r", "c", "l", "a", "p"]

I hope this helped you learn something new about the latest ES6! Happy coding!

Follow me on twitter @DuffinAvery

Im a software engineer, react developer, inventor, salesman, family man, religious, and outdoor connoisseur

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store