2

If I have

export const FOODS = {
  FRUITS: {/* ... */},
  RED_MEATS: {/* ...* },
  POULTRY: {/* ...* },
  VEGETALBES: {/* ... */},
}

export const CARNIVORE = {
  RED_MEATS,
  POULTRY,
}

Is there a quick way to export all the keys of FOODS without being explicit?

I would like to be able to do something like

export const {...} = FOODS;

The closest thing I can think would work is

export const {FRUITS, RED_MEATS, POULTRY, VEGETABLES} = FOODS;

But I have a large number of keys and would like something that requires less lines / less maintance

Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
  • `Object.keys(FOODS)`? – kind user May 03 '17 at 21:40
  • 2
    Why not just export `FOODS` as you are now, and let the importers decide what they want from that? – Heretic Monkey May 03 '17 at 21:41
  • 1
    @Kinduser That returns an array with the keys as strings... – Andrew Li May 03 '17 at 21:41
  • http://stackoverflow.com/questions/29844074/es6-export-all-values-from-object – mfirry May 03 '17 at 21:41
  • 1
    You shouldn't have used an object `FOODS` in the first place. You should write a module that exports `FRUITS`, `MEATS` and `VEGETABLES`, and then use `import * as FOODS from …` if you really need them contained in an object. – Bergi May 03 '17 at 21:55
  • @Bergi and I have other exports in the file as well that I wouldn't want to be includeded in foods (see update) – Seth McClaine May 03 '17 at 22:09
  • @MikeMcCaughan these exports are already being used I would have to updated 100's of lines to make that change – Seth McClaine May 03 '17 at 22:10
  • @SethMcClaine Then put the foods in a submodule that you can import separately. Basically in the module where you wanted to export them individually anyway. – Bergi May 03 '17 at 22:23

0 Answers0