React native标识对象中的布尔值

时间:2018-04-25 09:09:59

标签: reactjs react-native redux

我正在尝试为广告制作应用(IOS和Android),我希望能够进入广告对象并识别布尔值,如果确实如此,则做其他事情或其他事情。

Here are the objects of an ad:

我想进入该对象,如果" ReceiveHelp"是的,我希望代码执行说蓝色作为backgroundcoulor,否则为红色。问题是我不知道如何进入对象并仅识别布尔道具。

   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));

这是我目前必须将所有内容放入数组然后按时间排序的代码。但我不知道如何看布尔值是真还是假

2 个答案:

答案 0 :(得分:0)

您可以使用typeof

检查布尔值



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("The type is: ", typeof obj.receiveHelp));




一旦您知道receiveHelp是否为布尔值,您就可以使用if()轻松检查其真假:



const receiveHelp = true;

if (typeof receiveHelp === 'boolean') {
   if (receiveHelp) { // This equals to if (receiveHelp === true)
      console.log("this is true: ", receiveHelp);
   } else { // else false
      console.log("this is false: ", receiveHelp);
   }
}




答案 1 :(得分:0)

谢谢@Rodius

你帮助给了我一些灵感! 我解决了它,但做了:

let iftrue = '#666';
if (this.props.callbackFromParent.receiveHelp) {
   iftrue = '#ff0';

然后在我的广告风格中我已经" iftrue",没有意识到它是如此简单以至于#34; .receiveHelp"足以进入对象来检查值。