In my experiences to all the more likely comprehend Javascript I have gone over many astonishing (practical) answers for regular issues. Be that as it may, one has dependably befuddled me to no closure, well, until just as of late that is. Changing over an Array of Objects to an Object (in a decent practical manner!). 

The Problem 

First lets give a little piece of code… 

const peopleArray = [ 

{ id: 123, name: "dave", age: 23 }, 

{ id: 456, name: "Ankit", age: 23 }, 

{ id: 789, name: "sway", age: 23 }, 

{ id: 101, name: "tom", age: 23 }, 

{ id: 102, name: "tim", age: 23 } 

The bit is very straightforward, a variety of individuals objects where each question contains id, name, and age. 

For this issue lets accept you have been given the peopleArray and an id of the client and need to choose them from the peopleArray. 

What is Javascript?

The following is a bit of how you would ordinarily choose the client: 

let idToSelect = 789 

let selectedPerson 

for (let individual of peopleArray) { 

in the event that ( === idToSelect) { 

selectedPerson = individual; 


This is genuinely straightforward and will cross the cluster and dole out the right individual to selectedPerson. This presents numerous issues however, principally it brings transformation into your code. Alarming! 


How to Play Audio After Few Seconds or Delay in JavaScript

The Solution 

For me the perfect arrangement is convert the cluster into a protest that resembles the accompanying: 

const peopleObject = { 

"123": { id: 123, name: "dave", age: 23 }, 

"456": { id: 456, name: "Ankit", age: 23 }, 

"789": { id: 789, name: "sway", age: 23 }, 

"101": { id: 101, name: "tom", age: 23 }, 

"102": { id: 102, name: "tim", age: 23 } 

On the off chance that our information is composed in this organization you can just choose a man dependent on their id. Keeping the need to cross an exhibit!! 

const idToSelect = "789"; 

const selectedPerson = peopleObject[idToSelect]; 

This is much more clean, and expels change and intricacy from our code base. Be that as it may, by and large you will be unable to control the configuration that the information is displayed to you. So it might be best to change over that information on the fly. Fortunately ES6 gives us an approach to make this transformation. 

Note: If you have not found out about Array.reduce() this is the ideal opportunity to do as such. One of my past articles has connections to assets about utilizing Array.reduce() and in addition a model and basic clarification. 

const arrayToObject = (cluster) => 

array.reduce((obj, thing) => { 

obj[] = thing 

return obj 

}, {}) 

const peopleObject = arrayToObject(peopleArray) 


How to Build a Simple Chrome Extension in JavaScript

What we do in the scrap above is utilize diminish, which returns gatherer (obj), to which we add every thing in the exhibit utilizing that things id. This will change over our variety of items into a question of articles. Whoopee! (On the off chance that decrease is confounding you please go look at them here! They are freakin astounding!) 

The issue is that this bit is restricted to objects where the new protest key will be id. Lets dynamic this so the capacity is re-useable in all circumstances! 

const arrayToObject = (cluster, keyField) => 

array.reduce((obj, thing) => { 

obj[item[keyField]] = thing 

return obj 

}, {}) 

const peopleObject = arrayToObject(peopleArray, "id") 


The above code presently acknowledges a keyField which enables us to utilize this capacity to change over any variety of items. Not simply the individuals who have a the field id. This is vital for reusability of our code. 


By changing over our variety of items to a protest of articles, practically obviously, we have restricted the requirement for transformation in our codebase! On the off chance that you need more data on why it might be smarter to store your information in items rather than exhibits look at this article by Firebase. 

Much obliged! 

Much obliged for perusing! I'll be covering more themes sooner rather than later about revising fundamental Javascript ideas utilizing the new norms.

A Beginner’s Guide to JavaScript