0

I have recently come across this line of code.

const { Bar, Data: { Selectors } } = require('some-module');

Can someone please tell me what does Data: { Selectors } do in this piece of code? I understand that it is importing some module in my codebase, just confused about the {Selectors} part.

Also, if I want to write it using import syntax what would be the equivalent code for the same?

e.g : import Bar from "some-module"

Shouvik Bhuiyan
  • 75
  • 1
  • 1
  • 9

1 Answers1

2

It doesn't really have to do with modules (syntax is the same for import or require: import {Bar} from "some-module" ), it's just a destructuring assignment creating two variables, Bar and Selector.

Here's another example along with many other ES6 features http://es6-features.org/#ObjectMatchingDeepMatching

    const obj = {Bar: 1, Data: { Selectors: [1,2]}};
    const { Bar, Data: { Selectors } } = obj;
    // This is another ES6 feature, same as saying
    // console.log({Bar: Bar})
    console.log({Bar});
    console.log({Selectors});

The above is the same as saying

 const Bar = obj.Bar;
 const Selectors = obj.Data.Selectors
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217