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
CodePudding user response:
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)
CodePudding user response:
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)
})
CodePudding user response:
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);
