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

92
Views
Notación de punto/bracker - claves dinámicas

tengo un objeto Si profundiza en el objeto, tiene este objeto al que se accede a través de data[index].gallery.first.mobile/tablet/desktop

 "gallery": { "first": { "mobile": "./assets/product-xx99-mark-two-headphones/mobile/image-gallery-1.jpg", "tablet": "./assets/product-xx99-mark-two-headphones/tablet/image-gallery-1.jpg", "desktop": "./assets/product-xx99-mark-two-headphones/desktop/image-gallery-1.jpg" },

Estoy tratando de acceder a estas imágenes dinámicamente. Entonces, por ejemplo, tengo una función que devolverá ".mobile", etc., según el tamaño de la ventana. Sin embargo, no puedo obtener el retorno de imageSizer para agregar a data[index].gallery.first.{return from imageSizer required here} . Obtengo una mezcla de indefinido o [objeto objeto]. móvil, etc.

Además, a veces imageSizer devuelve .mobile y otras veces devuelve una matriz donde .mobile es el único elemento de la matriz y tiene un índice de 0. Depende si llamo a ImageSizer solo (devuelve ".mobile") o lo agrego a data[0].gallery.first devuelve [objeto objeto][matriz]

 const windowSizeInitial = window.innerWidth const imageSizer = (windowSize = windowSizeInitial) => { if(windowSize <= 600) { return ".mobile" } else if (windowSize <= 1400) { return ".tablet" } else { return ".desktop" } }

Aquí está la etiqueta de la imagen donde intento combinarlos para obtener la notación de puntos correcta necesaria para acceder a la imagen.

 src={data ? `${process.env.PUBLIC_URL}${data[0].gallery.first[imageSizer()]}` : ""} alt="product"

¿Es posible crear una notación de puntos dinámica como yo quiero?

¡Cualquier ayuda es apreciada! ¡Gracias!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sí, simplemente debes deshacerte del punto y regresar "mobile" , "tablet" y "desktop" .

P.ej:

 const foo = { bar: "hello" }; const key = "bar"; foo[key]; // "hello"
about 4 years ago · Juan Pablo Isaza 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!