我什至不确定是否可以这样做,但我会尝试解释我想要完成的任务。
我正在尝试过滤和映射一个数组,其中 labels.attributes.label === this.state.currentLabel
我正在让它发挥作用。但是如果 this.state.currentLabel === 'All'
我只想做 .map 而不是 .filter
我可以实现任何条件逻辑来使其正常工作吗?
以下是代码现在的样子:
{podcasts.filter((labels) => labels.attributes.label === this.state.currentLabel)
.map((pod) => {
const podId = pod.id.attributes['im:id']
const podName = pod['im:name'].label
return <PodcastItem key={podId} id={podId} name={podName} />
})}
抱歉,如果我解释得不好……我对编码还很陌生。因此,如果我能更好地描述它,请发表评论。
最佳答案
而不是像
那样链接let ans = data.filter(..)
.map(..)
你可以做到
let filtered = data;
if( /* your state condition */) {
filtered = data.filter(...)
}
let ans = filtered.map(...)
关于javascript - 如果状态是某种东西,则执行 .filter 和 .map,否则仅执行 .map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52767607/