1

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'. 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. 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.

13
  • what error are you having ? Commented Oct 8, 2018 at 6:46
  • Why email[i] ?? Commented Oct 8, 2018 at 6:46
  • 3
    You either need this.state.options[i] or just email . Commented Oct 8, 2018 at 6:47
  • 1
    Replace email[i] to email Commented Oct 8, 2018 at 6:48
  • 2
    @Rajesh it will have the same mistake as OP code. Commented Oct 8, 2018 at 6:52

1 Answer 1

2

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). Also, you'll need to wrap them in an element or you may also use 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:

!(arr[i].trim().toLowerCase().includes('no email alerts'))
Sign up to request clarification or add additional context in comments.

2 Comments

Just one doubt, so what is the email parameter mainly used for? I mean what I was thinking is email loops over the whole array and maps for each element?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.