Наш APIData
является массивом объектов, структура объекта такая.
{
"id": "DR001",
... fields,
"images": [
"http://placehold.it/1000x400/ffffff/c0392b/&text=slide1",
"http://placehold.it/1000x400/ffffff/c0392b/&text=slide2"
],
}
Нам нужно обернуть каждый объект компонентом Carousel
, поэтому сначала мы перебираем массив APIData
с помощью Array.map , который дает нам object
с каждой итерацией, затем мы циклически перебираем массив images
внутри этого объекта и визуализируем тег img
с источником изображения для каждого изображения вмассив.
// If all what we are doing is returning something from an arrow function
// we can omit the return statement and the {} and just wrap it in () or not wrap it
// at all but with () it is more readable
APIData.map(data => (
<Carousel key={data.id} {...settings}>
{data.images.map(imgSrc => (
<img key={imgSrc} src={imgSrc} alt={data.propertyFullName} />
))}
</Carousel>
));
Чтобы увидеть, что делает map
, вы можете выполнить этот код.
APIData.map((data, index) => {
console.log(`Outer map iteration number ${index + 1}`);
console.log('Outer map data', data);
console.log('Inner map within the outer map');
data.images.map(img => {
console.log(img);
});
});