Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
¿Cómo acceder a la matriz desde otro archivo .js?

Quiero obtener una matriz (muy grande) de otro archivo para que no ocupe espacio en mi archivo js principal, pero no puedo resolverlo, pasé entre 2 y 3 días buscando en línea pero no puedo encontrar una respuesta que lo haga trabajar. Probé las respuestas de Cómo acceder a la matriz js definida en otro archivo js, así como en muchos otros sitios, pero nada funciona.

 // the js file with the array I want wordList = [ "this", "is", "an", "array" ]
 // main js file const word = wordList[Math.floor(Math.random() * wordList.length)];

Cualquier ayuda es apreciada :) EDITAR: Esto es para un juego de navegador si eso ayuda

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede exportar la variable desde el primer archivo usando exportar.

 // example.js const WordList = [ "this", "is", "an", "array" ] export { WordList };

Luego, importe la variable en el archivo main.js usando import.

 import { WordList } from './example.js'

about 4 years ago · Santiago Trujillo Report

0

Debe agregar ambos scripts a html. Luego, debe hacer que la matriz sea global:

 window.wordList = [ "this", "is", "an", "array" ]

Luego, en el archivo main.js , puede acceder a la matriz de esta manera:

 const word = window.wordList[Math.floor(Math.random() * wordList.length)];

Pero asegúrese de agregar el script con la matriz antes del archivo main.js en el html

about 4 years ago · Santiago Trujillo Report

0

Debe exportar su matriz e importarla en cualquier archivo que desee. Los detalles completos del mecanismo de exportación/importación se pueden encontrar en la siguiente pregunta de Stackoverflow

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!