javascript - 尝试绘制没有重复的数据
问题描述
我是这里的初学者!我正在尝试使用此 data.JSON 按流派部分创建过滤,如下所示:
"movies": [
{
"title": "The Shawshank Redemption",
"release": "1994",
"rating": "9.2",
"genre": "Drama",
"id": 1
},
{
"title": "The Godfather",
"release": "1972",
"rating": "9.1",
"genre": "Crime Drama",
"id": 2
},
{
"title": "The Godfather Part II",
"release": "1974",
"rating": "9",
"genre": "Crime Drama",
"id": 3
},
{
"title": "The Dark Knight",
"release": "2008",
"rating": "9",
"genre": "Action",
"id": 4
},
{
"title": "Howl's Moving Castle ",
"release": "2001",
"rating": "9.5",
"genre": "Animation",
"id": 5
}]
下面是我当前对 map 方法的实现,但结果是类型有重复(两部犯罪剧)
我正在尝试过滤掉重复项。我该怎么做呢?
import useFetch from "./useFetch";
const MovieList = () => {
const { data, isPending, error, setData } = useFetch(
"http://localhost:8002/movies"
);
return (
<div className="movie-list">
<div className="row">
<div className="col-2">
<ul className="list-group">
<li className="list-group-item">All Genre</li>
{data.map((item) => {
return (
<li
key={item.id}
onClick={handleFilterGenre}
className="list-group-item"
>
{item.genre}
</li>
);
})}
</ul>
</div>
任何帮助深表感谢!谢谢
解决方案
试试这种方法:(一个包含已经编写的流派的外部数组 - 用于过滤)
import useFetch from "./useFetch";
const MovieList = () => {
const { data, isPending, error, setData } = useFetch(
"http://localhost:8002/movies"
);
const displayedGenres = [];
return (
<div className="movie-list">
<div className="row">
<div className="col-2">
<ul className="list-group">
<li className="list-group-item">All Genre</li>
{data.map((item) => {
if (displayedGenres.includes(item.genre))
return;
else {
displayedGenres.push(item.genre);
return (
<li
key={item.id}
onClick={handleFilterGenre}
className="list-group-item"
>
{item.genre}
</li>
);
}
})}
</ul>
</div>
推荐阅读
- c# - Android 10-System.NotSupportedException:无法从本机句柄激活 Xamarin.Forms.Platform.Android.Platform+DefaultRenderer 类型的实例
- swift - SwiftUI:在数组中更新值后视图没有改变
- python - AttributeError“SQLAlchemy”对象没有属性“创建”
- cakephp - 有什么方法可以跳过 CakePHP 3 中的 buildRules 函数检查
- facebook - Flutter App 在使用 facebook 登录时崩溃(仅在 Android 中)
- javascript - 使用正则表达式阻止脚本字符
- python - 关于peewee事务提交
- javascript - 仅当元素紧跟着另一个元素时才选择元素
- tfs - 无法在 MS Team Foundation Server 2015 上创建 CxSAST 构建步骤
- selenium - 屏幕截图中未呈现无头 Firefox 谷歌地图