Javascript 如何遍历react状态变量数组并确定它是否有一个真值
我有一个状态变量,如下所示Javascript 如何遍历react状态变量数组并确定它是否有一个真值,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我有一个状态变量,如下所示 const Home = ({ t, i18n, history }) => { const [otherStudiosAvailable, setOtherStudiosAvailable] = useState([]); const studios = ["de", "fr", "pl", "en", "nl", "ru"]; const otherStudios = studios.filter(item => { return
const Home = ({ t, i18n, history }) => {
const [otherStudiosAvailable, setOtherStudiosAvailable] = useState([]);
const studios = ["de", "fr", "pl", "en", "nl", "ru"];
const otherStudios = studios.filter(item => {
return item !== i18n.language;
});
useEffect(() => {
let others = [];
for (const studio of otherStudios) {
checkForAvailableAgent(
`sales_${studio}`,
"Linktostudio",
"serviceID"
)
.then(res => {
others[studio] = res;
setOtherStudiosAvailable(others);
})
.catch(error => {
console.log("an error happened.");
});
}
}, []);
}
{otherStudiosAvailable.indexOf(true) ? (
<Button
variant="success"
onClick={e => callBtnClick("flag")}
>
Other studios available
</Button>
) : ("")
}
consthome=({t,i18n,history})=>{
const[otherstudiosavable,setotherstudiosavable]=useState([]);
const studios=[“de”、“fr”、“pl”、“en”、“nl”、“ru”];
const otherStudios=studios.filter(项=>{
返回项!==i18n.language;
});
useffect(()=>{
让别人=[];
对于(其他工作室的const studio){
检查有效代理人(
`销售${studio}`,
“Linktostudio”,
“服务ID”
)
。然后(res=>{
其他(演播室)=res ;;
setOtherStudiosAvailable(其他);
})
.catch(错误=>{
log(“发生错误”);
});
}
}, []);
}
{otherStudiosAvailable.indexOf(true)(
callBtnClick(“flag”)}
>
其他工作室可用
) : ("")
}
我需要基于otherStudiosAvailable在JSX中进行条件检查,如果它至少有一个true值
如果数组中不存在true,则实际indeOf将返回-1 但是-1的truthy值为true,因此需要显式检查它是否不等于-1(这表示元素的存在) 值(otherStudiosAvailable)返回此对象中存在的值数组
{Object.values(otherStudiosAvailable).indexOf(true)!==-1 ? (
<Button
variant="success"
onClick={e => callBtnClick("flag")}
>
Other studios available
</Button>
) : ("")
}
{Object.values(otherStudiosAvailable).indexOf(true)!=-1(
callBtnClick(“flag”)}
>
其他工作室可用
) : ("")
}
如果数组中不存在true,则实际上indeOf将返回-1
但是-1的truthy值为true,因此需要显式检查它是否不等于-1(这表示元素的存在)
值(otherStudiosAvailable)返回此对象中存在的值数组
{Object.values(otherStudiosAvailable).indexOf(true)!==-1 ? (
<Button
variant="success"
onClick={e => callBtnClick("flag")}
>
Other studios available
</Button>
) : ("")
}
{Object.values(otherStudiosAvailable).indexOf(true)!=-1(
callBtnClick(“flag”)}
>
其他工作室可用
) : ("")
}
我提出了这个解决方案
{otherStudiosAvailable.indexOf(true)!==-1 && (
<Button
variant="success"
onClick={e => callBtnClick("flag")}
>
Other studios available
</Button>
)
}
{otherstudiosavable.indexOf(true)!=-1&&(
callBtnClick(“flag”)}
>
其他工作室可用
)
}
我提出了这个解决方案
{otherStudiosAvailable.indexOf(true)!==-1 && (
<Button
variant="success"
onClick={e => callBtnClick("flag")}
>
Other studios available
</Button>
)
}
{otherstudiosavable.indexOf(true)!=-1&&(
callBtnClick(“flag”)}
>
其他工作室可用
)
}
如果otherStudiosAvailable至少有一个true,则显示按钮,否则不显示任何内容我认为条件应该是otherStudiosAvailable。indexOf(true)!=-1,因为如果第一个元素(索引0)中存在true,则结果将为False,这与您的其他问题有何不同?如果otherStudiosAvailable至少有一个true,则显示按钮,否则不显示任何内容我认为条件应为otherStudiosAvailable。indexOf(true)!=-1,因为如果第一个元素(索引0)中存在true,则结果将为False,这与您的其他问题有何不同?log(otherstudiosavable)给出en:true fr:false nl:false pl:false ru:false但是我总是在indexOf中得到false(true)它是否返回一个对象数组?如果otherStudiosAvailable是一个值数组(不是对象),我的答案是有效的。进一步检查后,我了解otherStudiosAvailable是一个对象。我已根据itI更新了我的答案。我得到类型错误:otherStudiosAvailable.values(…)。indexOf不是function@akano1抱歉,我的错误是它的对象。值(otherStudiosAvailable)我已经更新了我的答案,这肯定会解决问题!!log(otherstudiosavable)给出en:true fr:false nl:false pl:false ru:false但是我总是在indexOf中得到false(true)它是否返回一个对象数组?如果otherStudiosAvailable是一个值数组(不是对象),我的答案是有效的。进一步检查后,我了解otherStudiosAvailable是一个对象。我已根据itI更新了我的答案。我得到类型错误:otherStudiosAvailable.values(…)。indexOf不是function@akano1抱歉,我的错误是它的对象。值(otherStudiosAvailable)我已经更新了我的答案,这肯定会解决问题!!