简体   繁体   English

javascript中的map函数不适用于React组件

[英]map function in javascript not working properly for React components

I am working on a project in ReactJs.I have an array in javascript for which the values will come from the back-end.So, the length of the array will be dynamic.So, I want my UI to change if the array elements contain the string 'No Email Alerts'. 我正在ReactJs中的一个项目上工作。我在javascript中有一个数组,其值将来自后端。因此,数组的长度将是动态的。所以,我希望我的UI如果数组元素发生变化包含字符串“无电子邮件警报”。 So I am writing a map on my array components and trying to render it in the following way: 因此,我在数组组件上编写了一个映射,并尝试通过以下方式呈现它:

 {this.state.options.map((email, i) => {
          return (
            <Something />   
                {!(email[i].includes('No Email Alerts')) ? (
                  <Some_Other_UI />
                ) :

                  null}

But, the issue I am facing now is I am not able to exclude the part which contains 'No Email Alerts' and render the different UI. 但是,我现在面临的问题是我无法排除包含“无电子邮件警报”的部分并呈现不同的UI。 I mean my 我的意思是我的

{!(email[i].includes('No Email Alerts')) ? (
                      <Some_Other_UI />
                    ) :

                      null}

is not working. 不管用。 What am i doing wrong? 我究竟做错了什么? Someone please guide me. 有人请指导我。

The map parameters email and i is used for this.state.options and the i can be used for this.state.options array not just with email (you could use if email has similar indexes though). 映射参数emaili用于this.state.options ,而i可以用于this.state.options数组,不仅用于email (不过,如果email具有相似的索引,则可以使用)。 Also, you'll need to wrap them in an element or you may also use fragment : 另外,您需要将它们包装在一个元素中,或者也可以使用fragment

{
 this.state.options.map((email, i, arr) => {
   return (
     <div key={email+'-'+i}>{/* provide the key as you wish */}
        <Something />   
        {!(arr[i].includes('No Email Alerts')) ? (
           <Some_Other_UI />
         ) :
        null}
     </div>
   )
}

I would also suggest to use trim and check for lower case in includes like this: 我也建议使用trim并检查是否包含小写字母,例如:

!(arr[i].trim().toLowerCase().includes('no email alerts'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM