Get first data in JSON API

Viewed 140

I'm relatively new in developing with React. I'm currently working on a Commodities Market Ticker. I'm using fetch() to get and render data from JSON API. Here's how the JSON looks:

[
  {
    Symbol: "BIT:COM",
    Ticker: "BIT",
    Name: "Bitumen",
    Country: "commodity",
    Date: "2020-09-30T00:00:00",
    State: "OPEN ",
    Last: 2020,
    Close: 2020,
    CloseDate: "2020-09-30T00:00:00",
    Group: "Industrial",
    URL: "/commodity/bitumen",
    Importance: 100,
    DailyChange: -40,
    DailyPercentualChange: -1.9417,
    WeeklyChange: -284,
    WeeklyPercentualChange: -12.3264,
    MonthlyChange: -560,
    MonthlyPercentualChange: -21.7054,
    YearlyChange: -1140,
    YearlyPercentualChange: -36.0759,
    YTDChange: -1054,
    YTDPercentualChange: -34.2876,
    day_high: 2062,
    day_low: 1930,
    yesterday: 2060,
    lastWeek: 2304,
    lastMonth: 2580,
    lastYear: 3160,
    startYear: 3074,
    decimals: 2,
    unit: "CNY/T",
    frequency: "Daily",
    LastUpdate: "2020-10-05T07:33:00"
  },
  {
    Symbol: "JBP:COM",
    Ticker: "Steel",
    Name: "Steel",
    Country: "commodity",
    Date: "2020-09-30T00:00:00",
    State: "OPEN ",
    Last: 3607,
    Close: 3607,
    CloseDate: "2020-09-30T00:00:00",
    Group: "Industrial",
    URL: "/commodity/steel",
    Importance: 100,
    DailyChange: -13,
    DailyPercentualChange: -0.3591,
    WeeklyChange: -15,
    WeeklyPercentualChange: -0.4141,
    MonthlyChange: -148,
    MonthlyPercentualChange: -3.9414,
    YearlyChange: -42,
    YearlyPercentualChange: -1.151,
    YTDChange: -135,
    YTDPercentualChange: -3.6077,
    day_high: 3628,
    day_low: 3591,
    yesterday: 3620,
    lastWeek: 3622,
    lastMonth: 3755,
    lastYear: 3649,
    startYear: 3742,
    decimals: 2,
    unit: "CNY/T",
    frequency: "Daily",
    LastUpdate: "2020-10-05T05:46:00"
    }
]

If you want to see the full JSON: https://api.tradingeconomics.com/markets/commodities/?c=guest:guest&format=json

This is how I render the data :

import React, {Component} from "react";
import List from './components/list'

class App extends Component{
    state = {
        data: []
    }

    componentDidMount() {
        fetch('https://api.tradingeconomics.com/markets/commodities/?c=guest:guest&format=json')
            .then(res => res.json())
            .then((data) => {
                this.setState({data: data})
            })
            .catch(console.log)
    }

    render() {
        return (
            <div className={'App'}>
                <List data={this.state.data}/>
            </div>
        )
    }
}

export default App;

And here's how I get the data:

import React from "react";

const List = ({ data }) => {
    return (
        <div>
            <h1>Commodities</h1>
            {data.map((value) => (
                <div>
                    <h5>Name: {value.Name}</h5>
                    <h5>Ticker: {value.Ticker}</h5>
                    <h5>High: {value.day_high}</h5>
                    <h5>Low: {value.day_low}</h5>
                    <h5>Unit: {value.unit}</h5>
                    <hr/>
                </div>
            ))}
        </div>
    )
}

export default List;

My question is, How to get the first data? Let's say that I want to show Ticker, Name and Date only from "BIT:COM". With my code now it's show all the data. Thank you in advanced.

2 Answers

You could do something like this :

const List = ({ data }) => {
    return (
        <div>
            <h1>Commodities</h1>
            {data.map((value) => (
               value.Name === "BIT:COM" ?
                <div>
                    <h5>Name: {value.Name}</h5>
                    <h5>Ticker: {value.Ticker}</h5>
                    <h5>High: {value.day_high}</h5>
                    <h5>Low: {value.day_low}</h5>
                    <h5>Unit: {value.unit}</h5>
                    <hr/>
                </div> : null
            ))}
        </div>
    )

}

There are two ways you can do this:

  1. If you only want to show BIT:COM
const List = ({ data }) => {
    return (
        <div>
            <h1>Commodities</h1>
            {data.filter(a => a.Symbol === "BIT:COM").map((value) => (
                <div>
                    <h5>Name: {value.Name}</h5>
                    <h5>Ticker: {value.Ticker}</h5>
                    <h5>High: {value.day_high}</h5>
                    <h5>Low: {value.day_low}</h5>
                    <h5>Unit: {value.unit}</h5>
                    <hr/>
                </div>
            ))}
        </div>
    )
}

  1. if you only want to show the first data:
const List = ({ data }) => {
    let value = data[0]
    return (
        <div>
            <h1>Commodities</h1>
            <div>
                <h5>Name: {value.Name}</h5>
                <h5>Ticker: {value.Ticker}</h5>
                <h5>High: {value.day_high}</h5>
                <h5>Low: {value.day_low}</h5>
                <h5>Unit: {value.unit}</h5>
                <hr/>
            </div>
        </div>
    )
}
Related