As a programmer you'll get into situations where you want to modify object values according to your need or convert array values into object key value pairs, so there are couple of examples here which could help you achieving so.
Using Spread(...) operator:
example: Converting array values into object properties and values
As you can see from below, I have used a declared array and converted the same using spread operator. so when you use spread operator to convert from array into object at that point of time ... converts all values of arrays inside into key value pairs and spreads it into an object. by default as you notice it takes keys values from the array index of individual elements
Using for...in Loop:
Using for...in loop is another way of playing with objects and converting values of individual properties as the properties of the object, yes you heard it right !!!, as you can see below I have an object countries with key value pairs and by using the for...in loop I have modified values of old object into the keys of new object. Here in this example I have again used spread operator to make sure not leaving behind a single value and making sure I have all the values in newCountries object.
There are so many more such examples, feel free to add in comments and keep learning :).
Top comments (0)