0

I'm trying packages many file objects into a module, and them import all of them... then iterate over all of them.

landingData.js:

import { clientLinks, linkName } from "../Utils/index.js";

const landing = {
  linkName: linkName.landing,
  link: internLinks.landing,
  keyWords: [],
  pageContent: {},
  externLinks: {},
  internLinks: {},
  imgAltNames: {},
  documents: {},
  videos: {}
}

export { landing }

aboutData.js:

import { clientLinks, linkName } from "../Utils/index.js";

const about = {
  linkName: linkName.about,
  link: internLinks.about,
  keyWords: [],
  pageContent: {},
  externLinks: {},
  internLinks: {},
  imgAltNames: {},
  documents: {},
  videos: {}
}

export { about }

contactData.js:

import { clientLinks, linkName } from "../Utils/index.js";

const contact = {
  linkName: linkName.contact,
  link: internLinks.contact,
  keyWords: [],
  pageContent: {},
  externLinks: {},
  internLinks: {},
  imgAltNames: {},
  documents: {},
  videos: {}
}

export { contact }

index.js:

import { landing } from "./landingData.js";
import { about } from "./aboutData.js";
import { contact } from "./contactData.js";

export {
  landing,
  about,
  contact
}

Now here is where I try to import the module dynamically and iterate over all the objects in the module.

import * as dataModule from "./Content/index.js"

console.log(dataModule); // Working, I can see all 3 objects in the module

// Need code here to iterate over objects in the module.

I can't seem to find anything related to what I am trying to do here. Anyone have any ideas?

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • What you're importing is a *module namespace object*. You can loop through it just like any other JavaScript object (`for-in`, `Object.keys`/`values`/`entries`, etc.). – T.J. Crowder Apr 30 '20 at 13:44
  • thanks, I had to mess with the syntax from the duplicate answer, but it helped... `for(let dataModuleKey in dataModule){console.log(dataModule[dataModuleKey].pageContent)}` – Fiddle Freak Apr 30 '20 at 13:51

0 Answers0