2

Suppose I have some JSON like below:

[
  {
    "date": "2020-12-25",
    "total_cases": 469482.0,
    "new_cases": 2260.0,
    "new_cases_smoothed": 2115.571,
    "total_deaths": 9816.0,
    "new_deaths": 63.0,
    "new_deaths_smoothed": 80.857,
    "total_cases_per_million": 2125.388,
    "new_cases_per_million": 10.231,
    "new_cases_smoothed_per_million": 9.577,
    "total_deaths_per_million": 44.438,
    "new_deaths_per_million": 0.285,
    "new_deaths_smoothed_per_million": 0.366,
    "new_tests": 54649.0,
    "total_tests": 6482889.0,
    "total_tests_per_thousand": 29.349,
    "new_tests_per_thousand": 0.247,
    "new_tests_smoothed": 38118.0,
    "new_tests_smoothed_per_thousand": 0.173,
    "positive_rate": 0.056,
    "tests_per_case": 18.0,
    "tests_units": "tests performed"
  },
  {
    "date": "2020-12-26",
    "total_cases": 471335.0,
    "new_cases": 1853.0,
    "new_cases_smoothed": 2006.714,
    "total_deaths": 9874.0,
    "new_deaths": 58.0,
    "new_deaths_smoothed": 77.714,
    "total_cases_per_million": 2133.777,
    "new_cases_per_million": 8.389,
    "new_cases_smoothed_per_million": 9.085,
    "total_deaths_per_million": 44.701,
    "new_deaths_per_million": 0.263,
    "new_deaths_smoothed_per_million": 0.352,
    "new_tests": 40953.0,
    "total_tests": 6523842.0,
    "total_tests_per_thousand": 29.534,
    "new_tests_per_thousand": 0.185,
    "new_tests_smoothed": 37101.0,
    "new_tests_smoothed_per_thousand": 0.168,
    "positive_rate": 0.054,
    "tests_per_case": 18.5,
    "tests_units": "tests performed"
  },
  {
    "date": "2020-12-27",
    "total_cases": 473309.0,
    "new_cases": 1974.0,
    "new_cases_smoothed": 2048.714,
    "total_deaths": 9929.0,
    "new_deaths": 55.0,
    "new_deaths_smoothed": 76.714,
    "total_cases_per_million": 2142.714,
    "new_cases_per_million": 8.936,
    "new_cases_smoothed_per_million": 9.275,
    "total_deaths_per_million": 44.95,
    "new_deaths_per_million": 0.249,
    "new_deaths_smoothed_per_million": 0.347,
    "new_tests": 33270.0,
    "total_tests": 6557112.0,
    "total_tests_per_thousand": 29.685,
    "new_tests_per_thousand": 0.151,
    "new_tests_smoothed": 36539.0,
    "new_tests_smoothed_per_thousand": 0.165,
    "positive_rate": 0.056,
    "tests_per_case": 17.8,
    "tests_units": "tests performed"
  },
  {
    "date": "2020-12-28",
    "new_tests": 32205.0,
    "total_tests": 6589317.0,
    "total_tests_per_thousand": 29.83,
    "new_tests_per_thousand": 0.146,
    "new_tests_smoothed": 36172.0,
    "new_tests_smoothed_per_thousand": 0.164,
    "tests_units": "tests performed"
  }
]

Out of this array of records in the JSON, I'm looking to grab the most recent instance of "total_cases". Here is the JavaScript I wrote to satisfy this:

const total_cases = (data[data.length - 1].total_cases);

Obviously, this doesn't work, because the last record in the array doesn't contain an instance of the "total_cases" key. I'm fetching from the source using HTTP GET, and it is updated daily, so sometimes the latest record has the keys I'm looking for, but other times I get a TypeError.

My question is if there is any way to find the index of the latest record that has a particular key I'm looking for, when the JSON has some inconsistencies like this. The alternative is to say hell with it and look for another source. I'm considering that option, because this is too much of a hassle to work with, but I'm still curious as to what could be done. Hope that makes sense.

Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
Haz-ctrl
  • 25
  • 4
  • please check this questions: https://stackoverflow.com/questions/33268863/find-last-matching-object-in-array-of-objects/49199917#49199917 you will find a lot of different ways – Sarabadu Dec 29 '20 at 14:09

3 Answers3

3

You could reverse the array and then use the Array.find method.

const total_cases = data
  .slice() // Since reverse() changes the array in place, we need a copy of the original array
  .reverse()
  .find(record => record.total_cases !== undefined)
  .total_cases;
Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
1

A possible way to do that would be by reversing the array and then use Array.prototype.find() to search for the most recent record with total cases.

data.reverse().find(r => r.total_cases);
Sai
  • 76
  • 3
0

You can do something like this, that I find easy to understand for you and for the next dev reading your code

data
  .sort((i1, i2) => i1.date > i2.date ? -1 : 1)
  .filter(item => Boolean(item.total_cases))
  [0].total_cases
arnaudambro
  • 2,403
  • 3
  • 25
  • 51