Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

73
Vistas
ES6 Dynamically Reference Import

Say I'm importing a few objects that all have the same method. How can I dynamically reference them, say by strings.

For example,

import { Foo } from 'path/to/foo';
import { Bar } from 'path/to/bar';
import { Baz } from 'path/to/baz';

const things = ['Foo', 'Bar', 'Baz'];

things.forEach(thing => {
  thing.doSomething();
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If it has to be strings you can do this:

import { Foo } from 'path/to/foo';
import { Bar } from 'path/to/bar';
import { Baz } from 'path/to/baz';

const things = {
'Foo': Foo, 'Bar': Bar, 'Baz': Baz
};

Object.keys(things).forEach(key => {
  things[key].doSomething();
});

If it doesn't have to be strings, you can simply create an array of your imported Objects

import { Foo } from 'path/to/foo';
import { Bar } from 'path/to/bar';
import { Baz } from 'path/to/baz';

const things = [Foo, Bar, Baz];

things.forEach(thing => {
 thing.doSomething();
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Just don't use string , use as you have imported like this:

import { Foo } from 'path/to/foo';
import { Bar } from 'path/to/bar';
import { Baz } from 'path/to/baz';

const things = [Foo, Bar, Baz];

things.forEach(thing => {
 thing.doSomething();
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda