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

216
Vistas
how to write a loop that only changes value in object

i m catching from firebase storage 3 different images (link).

I want create three object with key->value pair. Key is always the same (url) and value is link from storage. i m trying loop url: but it only display me last link

  let linkArray = []
  let linkObject = {}
  
  this.img.forEach((el) => {
    linkArray.push(linkObject)
    linkObject['url'] = el
  
    console.log(linkObject)
  })

results from first code is :

  • 0: {url: 'thirdLink'}
  • 1: {url: 'thirdLink'}
  • 2: {url: 'thirdLink'}

]

In code below is what I want to achieve, but it is written in vue and it would be a problem if I entered the fourth image (link) in the firebase storage

[
  { 
    link: {
       url: 'firstLink',
       scaledSize: { width: 30, height: 30 },
    },
    check:true
  },  
  {
    link: {
       url: 'secondLink',
       scaledSize: { width: 30, height: 30 },
    },
    check:true  
  },  
  {  
    link: {
      url: 'thirdLink',
      scaledSize: { width: 30, height: 30 },
    },
    check:true  
 },
], 

scaledSize and check are always the same so you can ignore them.

Any suggest, advice

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

With your log you are not logging the array. Push the structure you want and log the array.

  let linkArray = []
  
  this.img.forEach((el) => {
    linkArray.push({ 
    link: {
       url: el['url'],
       scaledSize: el.scaledSize,
    },
    check:true
  }
)
  })

  console.log(linkArray)
about 4 years ago · Juan Pablo Isaza Denunciar

0

It should be like this. Hope this might help

let linkArray = []

this.img.forEach((el) => {
  let linkObject = {}
  linkObject['url'] = el
  linkArray.push(linkObject)
  console.log(linkObject)
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

In JavaScript, objects are a reference type. So every time you push a new object into the array you should create new it (to avoid overwriting the old object you created initially, causing only display last link). See more at https://dmitripavlutin.com/value-vs-reference-javascript/

You can edit it like this:

        let linkArray = []
        
        this.img.forEach((el) => {
            let linkObject = {'url': el}
            linkArray.push(linkObject)
            console.log(linkObject)
        })
        console.log(linkArray);
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