0

For example, I have a Object with several properties.

const MasterObject = {
  item1: 'value1',
  itme2: 'value2',
  item3: 'value3',
}

I can export the msterObject by simply

export default MasterObject

What I was trying to do is to mix export, so I added:

Object.keys(MasterObject).map(o => { 
  export MasterObject[o];
})

But it is invalidate syntax; is there anyway for quick export all item from the master object as single module, so I can do mixed import like.

import MasterObject, {item1, item2} from MasterObject 
Weijing Jay Lin
  • 2,991
  • 6
  • 33
  • 53

2 Answers2

0

No, exports are static and there'e be no way to enforce that when exporting all the properties of an object. Of course, you could split the object into its component properties and export those individually:

export const item1 = "value1"
export const item2 = "value2"
export default const masterObj = { item1, item2 };
Richard Turner
  • 12,506
  • 6
  • 36
  • 37
0

One way to do it would be to use the old module.exports and import like you always do.

Test.js

const obj = {
    item1: 'value1',
    item2: 'value2',
    item3: 'value3',
}

Object.keys(obj).map(key => { 
    module.exports[key] = obj[key]
})

App.js

import { item1, item2 , item3 } from 'Test.js'
console.log(item1, item2, item3) // value1 value2 value3
Rainbow
  • 6,772
  • 3
  • 11
  • 28