简体   繁体   中英

Given Two Array of Objects in JavaScript, How to Modify the Data to a New Array of Objects based on the Requirement?

The below data are the two given array of objects. The first variable vacStatus contains the information regarding the vaccination status for the state, date and number of people getting vaccinated. The second variable census2020 contains the information regarding the state and total people.

let vacStatus = [
    {"st": "Connecticut", "date": "12/05/2021", "vac": 3031233},
    {"st": "Connecticut", "date": "12/06/2021", "vac": 3031723},
    {"st": "New Jersey", "date": "01/08/2022", "vac": 7554407},
    {"st": "New Jersey", "date": "01/09/2022", "vac": 7560007},
    {"st": "New Jersey", "date": "01/10/2022", "vac": 7562207},
    {"st": "New Jersey", "date": "01/11/2022", "vac": 7564407},
    {"st": "New York", "date": "01/09/2022", "vac": 16625152},
    {"st": "New York", "date": "01/10/2022", "vac": 16626152},
    {"st": "Pennsylvania", "date": "01/10/2022", "vac": 10208537},
    {"st": "Pennsylvania", "date": "01/11/2022", "vac": 10218537},
    {"st": "Rhode Island", "date": "01/11/2022", "vac": 961320},
    {"st": "Vermont", "date": "01/11/2022", "vac": 564218},
];

let census2020 = [
    {"st": "Connecticut", "ppl": 3605944},
    {"st": "Maine", "ppl": 1362359},
    {"st": "Massachusetts", "ppl": 7029917},
    {"st": "New Hampshire", "ppl": 1377529},
    {"st": "Rhode Island", "ppl": 1097379},
    {"st": "Vermont", "ppl": 643077},
];

Below is the result I would like to get from munipulating the two given Array of Objects above. Note that the r is the rate of people getting vaccinated based on the total number of people in that state (data from the census2020), and the most recent vaccine status data (data from vacStatus where the "date" is the closest date to today)

// Your result should look like:
let vacRate = [
    {"st": "Connecticut", "r": 0.8407570943974726},
    {"st": "New Jersey", "r": null},
    {"st": "New York", "r": null},
    {"st": "Pennsylvania", "r": null},
    {"st": "Rhode Island", "r": 0.8760145765501254},
    {"st": "Vermont", "r": 0.8773723830894279},
];

Below is my solution, but it is NOT correct!!!!

let vacRate = [];
for (i = 0; i < vacStatus.length; i++ ){
    let state = vacStatus[i]["st"]
  let vacPpl = vacStatus[i]["vac"]
  let tempData = {"st": state, "r": null};
  for (let each of census2020) {
    if(each["st"] == state){
        let pplInfo = each.ppl;
      tempData = {"st": state, "r": vacPpl / pplInfo};
      vacRate.push(tempData)
    }
  }
  vacRate.push(tempData)
}
console.log(vacRate)
console.log(vacRate.length)

Could you please help me with how to get the data right in vacRate? I admit that I am very bad at array of objects, but it is such a useful and frequent data structure to use.

I appreciate your help!!!

I don't know too much details of what you're trying to do but I could get the values like this:

const rate = vacStatus.map((status) => {
  const currentCensus = census2020.find((census) => census.st === status.st);
  const vacRate = status.vac / currentCensus?.ppl || null;

  return {
    st: status.st,
    r: vacRate,
  };
});

Do you need to sum up the "vac"s of same state and different dates before calculating the rate? if so, the approach needs to be different:

const sanitizedVacStatus = Array.from(
  vacStatus.reduce((m, { st, vac }) => m.set(st, (m.get(st) || 0) + vac), new Map()),
  ([st, vac]) => ({ st, vac })
);

First you need to sum up all the equal cities by the vac value and return only one item each (in this case, without the date).

And then you can use sanitizedVacStatus as the value for the first map instead of vacStatus .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM