0

I want to write a Ternary Operator below so the when this.props.location.state.programId is equal to r.programschosen.map(e => e.id) it outputs the name r.programschosen.map(e => e.program_name). How to write it below?

    {
      id: "program",
      label: "Program",
      customCell: r => (
        <td>
          {this.props.location.state.programId}
          {r.programschosen.map(e => e.program_name).join(", ")}{" "}
        </td>
      )
    },
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
john smith
  • 31
  • 1
  • 5
  • 2
    `this.props.location.state.programId` sounds like a string/number. `r.programschosen.map(e => e.id)` sounds like an array. How should they ever be equal? – MauriceNino Oct 27 '21 at 14:58
  • I've edited you question to replace the "What's the best way" text with "How". No one's going to answer with the worst way to do that :). Stack Overflow has rules about opinion-based questions, and asking for the "best way" invites opinions over just methods for solving the issue. Please be sure to phrase your questions in a neutral, objective fashion in the future. – Heretic Monkey Oct 27 '21 at 15:05
  • They are both numbers. this.props.location.state.programId = 28 and r.programschosen.map(e => e.id) = 5 & 28 – john smith Oct 27 '21 at 15:05
  • I think what you want is when `r.programschosen.map(e => e.id)` includes `this.props.location.state.programId`, then show the name. Are we to assume you're using React for this, since you're using `props` and JSX? – Heretic Monkey Oct 27 '21 at 15:10
  • Yes exactly. I am using JSX that's why I had to write it in that format. `this.props.location.state.programId` is from componentDidMount() function so when I go to a page it assigns an id which is `this.props.location.state.programId` but then I have an array that has a program_name + id in each index. What I want is if it sees that if it sees `this.props.location.state.programId` and id from `r.programschosen.map(e => e.id)` as equal then output the name related to that id :) Just not sure how to write it down – john smith Oct 27 '21 at 15:14
  • No need for a ternary, really. Just find the item in `programschosen` and if it's found, get the `program_name`. Something like `r.programschosen.find(e => e.id === this.props.location.state.programId)?.program_name`; the `?.` will return `null` if the item is not found. – Heretic Monkey Oct 27 '21 at 15:39

0 Answers0