I have an array of images and I wanted to them to be in ascending order.
My problem is I want the first image to be on the <CardMedia/>
while the other remaining images to be put on <img>
. What will be the most ideal way to do this using the latest version of JS?
Codesandbox -> CLICK HERE
Code
export default function BasicCard() {
const orderImages = images.sort(function (a, b) {
return a.order - b.order;
});
return (
<Card>
<Stack spacing={1}>
<CardMedia
component="img"
image={orderImages?.[0]?.url}
sx={{
maxHeight: { xs: 100, sm: 100 },
objectFit: "cover"
}}
/>
<Grid container spacing={0.5}>
{orderImages?.map((image) => (
<Grid key={image.order} item xs={4}>
<img src={image.url} width="100%" />
</Grid>
))}
</Grid>
</Stack>
</Card>
);
}
Expected Output below
Card Media
{
order: 0,
url: "https://source.unsplash.com/user/c_v_r/1900x800"
},
Img src
{
order: 1,
url: "https://html.com/wp-content/uploads/flamingo.jpg"
},
{
order: 5,
url:
"https://res.cloudinary.com/demo/image/upload/if_ar_gt_3:4_and_w_gt_300_and_h_gt_200,c_crop,w_300,h_200/sample.jpg"
}