I have an API that returns a list of brands which is an array of objects. I tried to filter out and kept only specific ones. I can see that it ran twice on the console, but when I tried to push those found brands to the brandNames array, I can't.
let brandNames = []
for (let j = 0; j < brands.length; j++) {
let code = brands[j]
let brands = {
api: 'brands',
filters: [`code:${code}`],
}
axios.post(window.URL, brands).then((response) => {
let brand = response.data.groups.filter((item) => {
return item.brands[0].code == code
})
console.log('brand', brand) //ran 2 times
if (brand) {
brandNames.push(brand[0].brands[0].name) // push into array
}
})
}
console.log(brandNames) // got [] empty
I have a feeling that things went out of context somewhere, but I have no idea where it is ....