I'm filling in an array with objects. Each time the for loop iterates, it's updating the previous items of the array.
I've tried with various types of loops like for-each, for and for of.
var arrResult = new Array;
for (let element of accessoriesToDisplay) {
var obj = {};
var obj = await AdaptiveCardImporter.accessoryCard(element.Name,
element.Price, element.ProductDescription, element.URL, element.ImgURL);
arrResult.push(obj);
}
This array should have 3 different objects at the end and not the array filled in with just the last one. This code was working before I moved the accessoryCard method outside the main js file.
Watcher:
You code concept is fine. You code should execute normally, but you problem lies with your await - something there isn't right. For example this recreation of your code works:
const getObj = e => new Promise(resolve => setTimeout(() => resolve({val:e}), 300)) async function main() { var arrResult = [] var accessoriesToDisplay = [1, 2, 3] for (let element of accessoriesToDisplay) { var obj = await getObj(element) console.log(obj) arrResult.push(obj); } console.log(arrResult) } main()
But your code does not. Try console.log(JSON.stringify(obj))
on each loop iteration to check if the object returned is actually the object you are looking for - chrome likes to help out by automatically updating the value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.