In: Computer Science
Part I: transformCase(original) and
transformCases(cases)
*
* Write functions to transform COVID case data into a new Object
format.
*
* The `transformCase(original)` function takes an Object like
so:
*
* {
* "Age Group": "20 to 29 Years",
* "Neighbourhood Name": "Humewood-Cedarvale",
* "Outcome": "ACTIVE",
* "Client Gender": "FEMALE",
* "Classification": "CONFIRMED",
* "FSA": "M6C",
* "Currently Hospitalized": "No",
* "Episode Date": "2020-09-11",
* "Assigned_ID": 17704,
* "Outbreak Associated": "Sporadic",
* "Ever Intubated": "No",
* "Reported Date": "2020-09-18",
* "Currently in ICU": "No",
* "Source of Infection": "Close contact",
* "_id": 161020,
* "Ever in ICU": "No",
* "Ever Hospitalized": "No",
* "Currently Intubated": "No"
* },
*
* And transforms the data in the `original` case Object into a new
Object
* that looks like this (see comments on right-hand side with
details):
*
* {
* id: 161020, // rename _id to id
* isActive: true, // true if Outcome is "ACTIVE", false
otherwise
* neighbourhood: "Humewood-Cedarvale", // rename Neighbourhood Name
to neighbourhood
* hospitalInfo: {
* current: {
* hospitalized: false, // true if Currently Hospitalized is Yes,
false otherwise
* inICU: false // true if Currently In ICU is Yes, false
otherwise
* intubated: false, // true if Currently Intubated is Yes, false
otherwise
* },
* historical: {
* hospitalized: false, // true if Ever Hospitalized is Yes, false
otherwise
* inICU: false // true if Ever In ICU is Yes, false otherwise
* intubated: false, // true if Ever Intubated is Yes, false
otherwise
* }
* }
* }
******************************************************************************/
function transformCase(original) {}
/*******************************************************************************
Part II: transformCases(cases) with
iteration
*
* The `transformCases(cases)` (NOTE: transformCase singular vs.
transformCases plural)
* takes an Array of case Objects, and returns a new Array of
transformed Objects,
* calling transformCase() on each.
*
* In your solution, make use of the following:
*
* - create a new empty Array to hold all the transformed
cases
* - use a for-loop or .forEach() method to loop over all Objects in
the of the cases Array
* - pass each case Object to your transformCase() function to get a
new Object
* - add the new Object to your new array
* - return the new array with all the transformed Objects
******************************************************************************/
function transformCases(cases) {}
/*******************************************************************************
Part III: transformCases(cases) with .map()
*
* Rewrite your transformCases() function a second time using the
Array .map() method
* see
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
*
* In your solution, make use of the following:
*
* - use the .map() method of the cases Array to create a new
Array
* - In the .map() method's function, call your transformCase()
function
* - return the Array created by the .map() method
******************************************************************************/
function transformCases2(cases) {}
Hello Champ!
Here is the solution for the question -
Every step is self explanatory, if you face any issue, feel free to ask.
Part-1: transformCase() Function -
function transformCase(o)
{ 
  var c = new Object();
  c.id=o["_id"];
  c.isActive = (o["Outcome"]==="ACTIVE")?(true):(false);        
  c.neighbourhood = o["Neighbourhood Name"];
  
  var curr = new Object();
      curr.hospitalized = (o["Currently Hospitalized"]==="No")?false:true;
      curr.inICU = (o["Currently in ICU"]==="No")?false:true;
      curr.intubated = (o["Currently Intubated"]==="No")?false:true;
  
  var histo = new Object();
      histo.hospitalized=(o["Ever Hospitalized"]==="No")?false:true;    
      histo.inICU=(o["Ever in ICU"]==="No")?false:true;
      histo.intubated= (o["Ever Intubated"]==="No")?false:true;
  
  var hospitalinfo = new Object();
  hospitalinfo.historical = histo;
  hospitalinfo.current = curr;
  c.hospitalInfo = hospitalinfo;
  return(c);
}
Part-2: transformCases() Function using Iteration -
function transformCases(cases){
    var tranformed_cases = [];
    cases.forEach(function f(item){
        var new_case = transformCase(item);
        tranformed_cases.push(new_case);
    });
    return tranformed_cases;
}
Part-3: transformCase() Function using map function -
function transformCases_withmap(cases){
    cases.map(function f(item){
        return transformCase(item);
    });
    return cases;
}
Hope It Helps!
Don't forget to Like ;)