Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to return a specific range of indexes from within in a JSON Array?

$
0
0

I have this array of objects that I retrieved from an API. It is continuously being updated everyday. The array consists of a timeseries of cases for everyday. I want to use this array to return the cases for each date for the last week. I have fetched the country name and filtered the array, so it only shows the user's country's array. I was thinking I can map across the dates of the array to find the last 7 indexes. Is there a way to do this?

useEffect(() => {    if (mappedLocation !== null) {      axios        .get("https://api.covid19india.org/v4/min/timeseries.min.json")        .then((response) => {          setEarliest2(response.data);          const stateArray = response.data.filter(            (item) => item.country === mappedLocation.stateShort          );
{"AN": {"dates": {"2020-03-26": {"delta": {"confirmed": 1        },"delta7": {"confirmed": 1        },"total": {"confirmed": 1        }      },"2020-03-27": {"delta": {"confirmed": 5        },"delta7": {"confirmed": 6        },"total": {"confirmed": 6        }      },"2020-03-28": {"delta": {"confirmed": 3        },"delta7": {"confirmed": 9        },"total": {"confirmed": 9        }      },"2020-03-29": {"delta7": {"confirmed": 9        },"total": {"confirmed": 9        }      },"2020-03-30": {"delta": {"confirmed": 1        },"delta7": {"confirmed": 10        },"total": {"confirmed": 10        }      },"2020-03-31": {"delta7": {"confirmed": 10        },"total": {"confirmed": 10        }      },"2020-04-01": {"delta7": {"confirmed": 10        },"total": {"confirmed": 10        }      },  ...}
const DataSet = (props) => {return [    {      x: new Date(        parseInt(moment().format("YYYY")),        parseInt(moment().format("M")),        parseInt(moment().format("DD")) - 7      ),      y: props.last7DaysAN[7].confirmed, //?    },    {      x: new Date(        parseInt(moment().format("YYYY")),        parseInt(moment().format("M")),        parseInt(moment().format("DD")) - 6      ),      y: props.last7DaysAN[6].confirmed,    },    {      x: new Date(        parseInt(moment().format("YYYY")),        parseInt(moment().format("M")),        parseInt(moment().format("DD")) - 5      ),      y: props.last7DaysAN[5].confirmed,    },    {      x: new Date(        parseInt(moment().format("YYYY")),        parseInt(moment().format("M")),        parseInt(moment().format("DD")) - 4      ),      y: props.last7DaysAN[4].confirmed,    },    {      x: new Date(        parseInt(moment().format("YYYY")),        parseInt(moment().format("M")),        parseInt(moment().format("DD")) - 3      ),      y: props.last7DaysAN[3].confirmed,    },    {      x: new Date(        parseInt(moment().format("YYYY")),        parseInt(moment().format("M")),        parseInt(moment().format("DD")) - 2      ),      y: props.last7DaysAN[2].confirmed,    },    {      x: new Date(        parseInt(moment().format("YYYY")),        parseInt(moment().format("M")),        parseInt(moment().format("DD")) - 1      ),      y: props.last7DaysAN[1].confirmed,    },  ];}}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>