Search code examples
reactjsgraphqlapollo-client

Is it possible to output with specific index?


I want to get specific variable of amount.

Is it possible to say something, like: if index == 1 then return first amount? where possible amounts are: "3333" "444" "555"
If it is possible, how can I do it in the code?

query:

items{
    name
    amount
  }
const { data } = useQuery(ITEMS)

{data.items.map((items, index) => ( <p>{items.amount}</p>))}

Solution

  • Yes, do this, and you can replace the 1 with whatever index you want (remember, indexing starts at 0, so this 1 means the second value. if you want the first, replace it with 0).

    <p>{data.items[1].amount}</p>