Object Literals

keep in mind...

  1. you can still do it the old way, but shouldn't!
  2. way simpler way to create objects
  3. can even be used to merge objects! (shallow)
shorthandshallow merge

ES6 makes working with objects way easier than before.

shorthand

syntax Whereas we used to write out object by adding each key and value inside the object,
const obj = { id: id, color: color, name: name }

edit and re-run!
const id = 3, color = 'red', name = 'pete';

const obj = { id: id, color: color, name: name };

console.log(obj);
{"id":3,"color":"red","name":"pete"}

Now we can just write a shorthand version:
const obj = { id, color, name };

edit and re-run!
const id = 3, color = 'red', name = 'pete';

const obj = { id, color, name };

console.log(obj);
{"id":3,"color":"red","name":"pete"}

shallow merge

merge with spread For simple objects you can easily merge them in ES6 by using the spread operator. Beware: this does not work for complex objects. See this David Walsh article on deep merge for that.

edit and re-run!
const colorObj = { 'hue':'purple', 'sat': 'dark', 'lum': 'bright' };
const nameObj = { 'name ': 'happy'};

const obj = { ...colorObj, ...nameObj };

console.log(obj);
{"hue":"purple","sat":"dark","lum":"bright","name ":"happy"}

Research More...

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
  2. https://davidwalsh.name/merge-objects