1

Hi guys I'm trying to filter out the first iteration of the map function because its undefined and I don't want it to show,

the code is this:

  const data = this.state.arr.map((r, index) => (
      <tr key={index}>
        <th>{index}</th>
        <th>{r.formName}</th>
        <th>{this.state.numofSub} </th>
        <th>
          <Link to={`/submitPage/${r._id}`}>Submit PageLink</Link>
        </th>
        <th>
          <Link to={`/submissionPage/${r._id}`}>Submission page Link </Link>
        </th>
      </tr>
    ));

I tried adding if statement before the render like so:

  const data = this.state.arr.map((r, index) => (
  if(r._id){
      <tr key={index}>
        <th>{index}</th>
        <th>{r.formName}</th>
        <th>{this.state.numofSub} </th>
        <th>
          <Link to={`/submitPage/${r._id}`}>Submit PageLink</Link>
        </th>
        <th>
          <Link to={`/submissionPage/${r._id}`}>Submission page Link </Link>
        </th>
      </tr>
    )}
else return NULL
);

but it won't compile, can someone tell me how is the proper way to do this? to be clear I just want to check that the r._id has something in it, if it doesn't then I don't want it to show.

3 Answers 3

2

Your map function doesn't have a body (curly braces) {}.
And you forgot a return after the if statement.

 const data = this.state.arr.map((r, index) => {
  if(r._id){
    return(
      <tr key={index}>
        <th>{index}</th>
        <th>{r.formName}</th>
        <th>{this.state.numofSub} </th>
        <th>
          <Link to={`/submitPage/${r._id}`}>Submit PageLink</Link>
        </th>
        <th>
          <Link to={`/submissionPage/${r._id}`}>Submission page Link </Link>
        </th>
      </tr>
    )
   }
    else return null
})

Note that this will return an array with null in it. If you need to filter then chain filter before the map

Sign up to request clarification or add additional context in comments.

3 Comments

this code wont compile, I even tried copying and same error.
@AlexAlex i had a missing closing ) in the end. Edited
{ brackets are not mandatory. map works even with (. return is required when we use { brackets and return isn't required when we use ( bracket
0

You can use ternary operator and render JSX elements directly in render using .map

render(){
 const { arr, numofSub } = this.state;
 return(
    {arr && arr.map((r, index) => (
      {r._id ? (
      <tr key={`${r.formName}-${index}`}>
       <th>{index}</th>
       <th>{r.formName}</th>
       <th>{numofSub} </th>
       <th>
        <Link to={`/submitPage/${r._id}`}>Submit PageLink</Link>
      </th>
      <th>
         <Link to={`/submissionPage/${r._id}`}>Submission page Link </Link>
      </th>
    </tr>) : null}
))})
}

Comments

0

I think the Array.prototype.filter() method is quite effective to resolve issues like this. Something like this:

const filteredData = this.state.arr.filter(item => item.id !== undefined)
const data = filteredData.map((r, index) => (
  ...
)

No if statement needed with this setup.

Comments

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.