Within the backend, there exists an endless list of cars. Each car is designated by a unique id and has a corresponding model name.
I possess a compilation of car IDs, as illustrated below:
const carIds = ['abc','xyz'];
My objective is to retrieve the names that correspond to the aforementioned car IDs. Although I initially attempted the following code, it unfortunately failed to produce the desired results. Could you please identify what might be missing?
const carIds = ['abc','xyz']; // unique ids
const carsList = [];
for (const carId of carIds) {
this.api.getCar(carId).pipe(
map(response => {
carsList.push({
name: response.name,
id: carId
});
})
);
}
console.log(carsList); // Despite executing this code snippet, no output is generated.
The anticipated output should resemble the following structure:
carsList = [{
id: 'abc',
name: 'benz'
},{
id: 'xyz',
name: 'mercede'
}]
Your assistance is greatly appreciated.