Get the Price from an array and pass to a valuables for future calculations React

Solution for Get the Price from an array and pass to a valuables for future calculations React
is Given Below:

I am getting an array for Price List from API. There are 3 attributes I want to get separately for future calculation. Is it possible to get the values following values separately?

  • advancedCommission
  • Box Seats – Ground Floor’s adultPrice
  • ODC’s adultPrice
  • ODC’s childPrice

following is my data array

  "data": {
    "ticketPrices": {
      "advancedCommission": 60,
      "priceList": [
        {
          "id": 537,
          "seatType": "Box Seats - Ground Floor",
          "nonReserved": 0,
          "adultPrice": 800,
          "adultCommission": 0,
          "childPrice": 0,
          "childCommission": 0,
          "servicePrice": 0
        },
        {
          "id": 536,
          "seatType": "ODC",
          "nonReserved": 0,
          "adultPrice": 350,
          "adultCommission": 0,
          "childPrice": 280,
          "childCommission": 0,
          "servicePrice": 0
        }
      ]
    }
  },

following is my code

componentDidMount() {
    
    const requestOptions = {
        method: 'GET',
        headers: { 'Content-Type': 'application/json', 'client_token': 'ebd86470-7e90-4ece-9e89-1b6d4d2cbb61' }
      };
      fetch(`http://sandbox-api.com/v1/seat/prices?venue_id=90&movie_id=3232&showtime_id=2130&movie_date=2021-08-02`, requestOptions)
          .then(response => response.json())
          .then((seatPrice) => {
            console.log(seatPrice.data.ticketPrices.priceList)
            this.setState({ 
                seatPrice: seatPrice.data.ticketPrices.priceList,
                dataLoaded: true,
             })
          }).catch(console.log);

  }

Yes you can achieve this by using filters

const filteredODCData = seatPrice.data.ticketPrices.priceList.filter(type => type.seatType.includes("ODC"));
const adultPrice = filteredODCData[0].adultPrice;
const childPrice= filteredODCData[0].childPrice;


const filteredBoxData = seatPrice.data.ticketPrices.priceList.filter(type => type.seatType.includes("Box Seats - Ground Floor"));
const adultBoxPrice = filteredBoxData[0].adultPrice;

const advanceCommision = seatPrice.data.ticketPrices.advancedCommission;

If you want to use the values in other function, you can save it to state as follows

componentDidMount() {
    
    const requestOptions = {
        method: 'GET',
        headers: { 'Content-Type': 'application/json', 'client_token': 'ebd86470-7e90-4ece-9e89-1b6d4d2cbb61' }
      };
      fetch(`http://sandbox-api.com/v1/seat/prices?venue_id=90&movie_id=3232&showtime_id=2130&movie_date=2021-08-02`, requestOptions)
          .then(response => response.json())
          .then((seatPrice) => {
            console.log(seatPrice.data.ticketPrices.priceList);
            const filteredODCData = seatPrice.data.ticketPrices.priceList.filter(type => type.seatType.includes("ODC"));
            const adultPrice = filteredODCData[0].adultPrice;
            const childPrice= filteredODCData[0].childPrice;


            const filteredBoxData = seatPrice.data.ticketPrices.priceList.filter(type => type.seatType.includes("Box Seats - Ground Floor"));
            const adultBoxPrice = filteredBoxData[0].adultPrice;

            const advanceCommision = seatPrice.data.ticketPrices.advancedCommission;
            this.setState({ 
                seatPrice: seatPrice.data.ticketPrices.priceList,
                adultPrice: adultPrice,
                childPrice: childPrice,
                adultBoxPrice: adultBoxPrice,
                advanceCommision: advanceCommision,
                dataLoaded: true,
             })
          }).catch(console.log);

  }

and in your other function you can get the values as

const adultPrice = this.state.adultPrice;
const childPrice= this.state.childPrice;
const adultBoxPrice= this.state.adultBoxPrice;
const advanceCommision = this.state.advanceCommision ;