Reactjs 在对象中标识布尔值
我正在尝试为广告制作一个应用程序(IOS和Android),我希望能够进入我的广告对象,识别一个布尔值,如果它是真的,那么做一件事,做其他事情 我想进入对象,如果“ReceiveHelp”为真,我希望代码执行say blue as backgroundcoulor,否则为red。问题是,我不知道如何进入对象并只识别布尔属性Reactjs 在对象中标识布尔值,reactjs,react-native,redux,Reactjs,React Native,Redux,我正在尝试为广告制作一个应用程序(IOS和Android),我希望能够进入我的广告对象,识别一个布尔值,如果它是真的,那么做一件事,做其他事情 我想进入对象,如果“ReceiveHelp”为真,我希望代码执行say blue as backgroundcoulor,否则为red。问题是,我不知道如何进入对象并只识别布尔属性 export const publicAdFetch = () => { return (dispatch) => { fireba
export const publicAdFetch = () => {
return (dispatch) => {
firebase.database().ref('/users').once('value').then((snapshot) => {
const usersData = snapshot.val();
let sortedAdds = Object.keys(usersData).reduce((prev, userId) => {
let ads = usersData[userId].ads;
ads = Object.keys(ads).map(key => {
return { ...ads[key], id: key };
});
return prev.concat(ads);
}, [])
.sort((a, b) => (b.time - a.time));
这是我目前必须将所有内容放入数组,然后按时间排序的代码。但是我不知道如何判断布尔值是真是假你可以用
const yourObjArray=[
{desc:“fdf”,price:“rrr”,receiveHelp:true},
{desc:“ccc”,price:“254”,receiveHelp:351},
{desc:“aaa”,price:“gdg”,receiveHelp:false},
{desc:“aaa”,price:“gdg”,receiveHelp:“charlie”},
{desc:“feee”,price:“jth”,receiveHelp:true},
];
yourObjArray.forEach(obj=>console.log(“类型为:”,typeof obj.receiveHelp))代码>谢谢@Rodius
你的帮助给了我一些灵感!
我解决了它,但做了:
let iftrue = '#666';
if (this.props.callbackFromParent.receiveHelp) {
iftrue = '#ff0';
然后在我的广告风格中,我使用了“iftrue”,没有意识到它是如此简单,“.receiveHelp”足以进入对象检查值。发布代码。您可以在一行中使用:const color=this.props.callbackFromParent.receiveHelp?”#ff0':'#666'
。很高兴我能帮忙。