Getting to Grips with ES6: Destructuring

Zulash

Senior Member
Joined
Jan 22, 2007
Messages
699
Reaction score
2
Location
Cape Town
Hey guys,

I am writing a small series on ES6 and thought I would drop a link to my latest article:

Getting to Grips with ES6: Destructuring

I'm relatively new to this "writing" game so feel free to be as critical as you want of these articles... your feedback will help me iron out any issues and helps with better content in the future.

Thanks!
 
Nice work Andrew

I think that explains it quite well.

Destructuring is probably the most confusing feature for newcomers.

The bad API is a classic example. I just wish there was a way to easily "map" an object into another object using destructuring syntax, instead of having to do it into variables.
 
Nice work Andrew

I think that explains it quite well.

Destructuring is probably the most confusing feature for newcomers.

The bad API is a classic example. I just wish there was a way to easily "map" an object into another object using destructuring syntax, instead of having to do it into variables.
What about using a reference nested object as map.

Here's an example with underscore.js
PHP:
var city = {
  x: 'Cape Town',
  y: '987007',
  z: 'capetown.gov.za',
};

var map = {
    x : "name",
    y : "population",
    z : "website"
};

var city2 = {};

_.each(city, function(value, key) {
    key = map[key] || key;
    city2[key] = value;
});

Natively you could do the same with a for loop, e.g.
PHP:
for (key in map) city2[map[key]] = city[key];
Should be relatively easy to adapt for deeper nested objects.
 
Top
Sign up to the MyBroadband newsletter
X