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

207
Views
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 answers
Answer question

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 Report

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 Report

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 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!