gpt4 book ai didi

javascript - 数组中的图像排序

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:03 25 4
gpt4 key购买 nike

这是一个模拟。如何将此图像存储到数组中并根据最近和最喜欢的顺序进行排序。

我需要设置一个id吗?感谢您的帮助。

</style>



<figure class="einzel"><img alt="Mitglieder" src="https://unsplash.it/300/225?image=0" style="width: 315px; height: 250px;">
<figcaption>Name:<br>
<span>Likes:</span></figcaption>
</figure>

<figure class="einzel"><img alt="Mitglieder" src="https://unsplash.it/300/225?image=1" style="width: 315px; height: 250px;">
<figcaption>Name:<br>
<span>Likes:</span></figcaption>

</figure>

<figure class="einzel"><img alt="Mitglieder" src="https://unsplash.it/300/225?image=2" style="width: 315px; height: 250px;">
<figcaption>Name:<br>
<span>Likes:</span></figcaption>
</figure>

<figure class="einzel"><img alt="Mitglieder" src="https://unsplash.it/300/225?image=3" style="width: 315px; height: 250px;">
<figcaption>Name:<br>
<span>Likes:</span></figcaption>
</figure>

<figure class="einzel"><img alt="Mitglieder" src="https://unsplash.it/300/225?image=3" style="width: 315px; height: 250px;">
<figcaption>Name:<br>
<span>Likes:</span></figcaption>
</figure>

<figure class="einzel"><img alt="Mitglieder" src="https://unsplash.it/300/225?image=3" style="width: 315px; height: 250px;">
<figcaption>Name:<br>
<span>Likes:</span></figcaption>

</figure>



最佳答案

做这样的事情。首先创建一个包含数据的对象数组。对数组进行排序并对其进行循环并添加到 html。

const imgArr = [
{ src: "https://unsplash.it/300/225?image=0", Name: "Something1", Likes: 6 },
{ src: "https://unsplash.it/300/225?image=0", Name: "Something2", Likes: 2 },
{ src: "https://unsplash.it/300/225?image=0", Name: "Something2", Likes: 3 },
{ src: "https://unsplash.it/300/225?image=0", Name: "Something2", Likes: 1 },
];

const html = imgArr.sort((a, b) => {
return a.Likes - b.Likes
}).map(imageItem => {
return `<figure class="einzel"><img alt="Mitglieder" src=${
imageItem.src
} style="width: 315px; height: 250px;">
<figcaption>Name: ${imageItem.Name}<br>
<span>Likes: ${imageItem.Likes}</span></figcaption>
</figure>`;
});

document.getElementById("app").innerHTML = html;
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
<script src="src/index.js">
</script>
</body>
</html>

关于javascript - 数组中的图像排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53511719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com