我是编码的新手,我正在做一个个人项目,我被api过滤卡住了。我的要求是,如果数据中的数据等于一个特定的单词,例如:-nknown_for_department==";导演";如果这是真的,它应该呈现其中的数据。怎么能在reactjs中做到呢。有人能帮我吗?
{
"adult": false,
"gender": 2,
"id": 1787802,
"known_for_department": "Directing",
"name": "Brian Avery Galligan",
"original_name": "Brian Avery Galligan",
"popularity": 0.63,
"profile_path": null,
"credit_id": "5fd051d968929c003bc48070",
"department": "Directing",
"job": "Second Unit First Assistant Director"
},
{
"adult": false,
"gender": 1,
"id": 1774412,
"known_for_department": "Production",
"name": "Rachel O'Connor",
"original_name": "Rachel O'Connor",
"popularity": 0.6,
"profile_path": null,
"credit_id": "6124530a0f2fbd004815b6cb",
"department": "Production",
"job": "Executive Producer"
},
这是我拥有的api数据,如果known_for_department==directing,我应该从中过滤,如果它是真的,我应该将其中的所有项目呈现在页面中。有人能帮我吗?提前谢谢:(
这里有两个选项:
A。创建您的API请求,基于您需要的关键字(好(
B。在客户端筛选数据(错误(。在这种情况下,你的代码将是较慢的
let inputData = [
{
"adult": false,
"gender": 2,
"id": 1787802,
"known_for_department": "Directing",
"name": "Brian Avery Galligan",
"original_name": "Brian Avery Galligan",
"popularity": 0.63,
"profile_path": null,
"credit_id": "5fd051d968929c003bc48070",
"department": "Directing",
"job": "Second Unit First Assistant Director"
},
{
"adult": false,
"gender": 1,
"id": 1774412,
"known_for_department": "Production",
"name": "Rachel O'Connor",
"original_name": "Rachel O'Connor",
"popularity": 0.6,
"profile_path": null,
"credit_id": "6124530a0f2fbd004815b6cb",
"department": "Production",
"job": "Executive Producer"
},
]
let outputData = []
for (const element of inputData){
if (element["department"] === "Directing"){
outputData.push(element)
}
}
console.log(outputData)
在返回语句中执行以下操作:
{known_for_department === directing && <something/> }
&;只有当操作符的剩余部分是真的时,它才会被执行(这里:rendered(。
例如:
返回(
{api_data.known_for_department === api_data.directing && ({api_data.map((element) => <p>{element}</p>)}
)
然后,这将呈现api_data的所有属性的段落。
如果你想在条件错误的情况下呈现不同的东西,你可以使用三元运算符,比如
{known_for_department === directing ? <something/> : <something_else/> }
如果您只想在条件为true的情况下调用return语句,请写下:
if (known_for_department === directing) {
return (something...)
}