Merge Two Objects And Array To Object In JavaScript

How two merge two objects or transform an array into an object in JavaScript

Image for post
Image for post
Photo by Ludovic Migneault on Unsplash

Merge Two Objects

Thanks to the introduction of Spread Operator in ES6, it is now more than ever before really easy to merge two objects. No more loops, comparison or rocket science, the merge operation can be written in a single line of code.

const bruno = {
sing: true,
song: 'Treasure'

const ratm = {
type: 'band',
song: 'Bombtrack'

const result = {...bruno, ...ratm};


// -> {sing: true, song: "Bombtrack", type: "band"}

Array To Object

In order to transform an array to an object in JavaScript, I did not find so far the magic line of code which would do all the job in one single line of code. If you do know how to achieve this, ping me with your comments and best pieces of code, I would love to hear about it 😁.

// {name: string, genre: string}[]
const bands = [
name: 'Ratm',
genre: 'rock'
name: 'Bruno',
genre: 'Pop'
// {ratm: string, bruno: string}
const artists = bands.reduce(
(obj, item) => {
obj[] = item.genre;
return obj;


// {Ratm: "rock", Bruno: "Pop"}


It is interesting to notice that the more the language evolves, the more basic operations become easy to implement with few lines of code. Also interesting is the fact that sometimes it takes time to these new options to be rolled out to me. ES6 has been around for a while and I am still writing about it in 2020 😉.

Freelancer by day | Creator of DeckDeckGo by night | Organizer of the Ionic and IndieHackers Zürich Meetup

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