Accessing certain item after fetching data from API with react

Cuzto

I am learning react and stumbled on something that seems like an absolute beginner problem. Anyway I am fetching data from an API and would like to know how to get a certain element from the JSON. I have tried different variations with [] but with no success.

JSON:

[{"name":"gfhf","id":1,"organizer":"hfgh"},{"name":"World Cup","id":2,"organizer":"FIFA"}]

React code:

import React, { Component } from "react";
import Tournaments from "./Tournaments";

const tournyAPI = 'http://localhost:8080/api/tournaments';

class template extends Component {

  constructor() {
    super();
    this.state = {
      data: [],
    }
  }

  componentDidMount() {
    fetch(tournyAPI)
    .then((Response) => Response.json())
    .then((findresponse) => {
      console.log(findresponse)
      this.setState({
        data:findresponse,
      })
    })
  }

  render() {
    return (
      <div class="container">
        <div class="row">
          <div class="col-md-6 col-md-offset-3">
            <div class="jumbotron text-center">
            {
              this.state.data.map((dynamicData, key) =>
              <div>
                <h1>{dynamicData.name}</h1>
              </div>
              )
            }
          </div>
        </div>
      </div>
    </div>
    );
  }
}


export default template;

My goal would be to display only World cup for example.

ivp

You can filter out whatever you want from the response from the API. Like below:

Notice the line findresponse.filter(res => res.name === "World Cup");

componentDidMount() {
  fetch(tournyAPI)
  .then((Response) => Response.json())
  .then((findresponse) => {
    console.log(findresponse)
    findresponse.filter(res => res.name === "World Cup");
    this.setState({
      data:findresponse,
    })
  })
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Applying Filter after Fetching data from api in React

Fetching data from api in react

Looking to delete a list item from an array of items after fetching list data from REST API

Fetching data from api promise in react API

Data is not showing in app after fetching from an api

API accessing certain values from data with JavaScript

Fetching data from YouTube API with React Hooks

React Router Fetching data from API

Fetching data from api and passing it to state in React

Fetching API data with React

Fetching data from an api with interpolation to search weather in a certain city

React: following DRY in React when fetching data from API

Filtering data after fetching in React

setState not working in react native after fetching data from database

Render a functional component after fetching data from a POST API?

Fetching API data via React

Fetching data from nested array in API React Native

React.js: Fetching Data from API with Bearer Token

fetching POST data from API (node js, react)

React useEffect infinite loop fetching data from an api

Gettin 'null' while fetching data from api with React Context

Fetching data from API in react native using redux

Fetching random data from API on button click - React

fetching nested data from rest countries API using typescript in react

Getting an empty array in React while fetching the data from the api

React, fetching from an API logs the data, but rendering it doesn't?

React fetching Data from Node Server using fetch() API

Data structure changes when fetching from Flask backend with react API

data is not fetching from useLocation in react