Home > other >  How do I sort an object based on an attribute in an array of objects
How do I sort an object based on an attribute in an array of objects

Time:02-03

I want to sort an object called metadata below:

const metadata = {
  glucose: {
    name: 'Glucose',
    units: 'mg/dL',
  },
  height: {
    name: 'Height',
    units: '"',
    longUnit: 'Inches',
  },
  weight: {
    name: 'Weight',
    units: 'lbs',
    longUnit: 'Pounds',
  },
  bmi: {
    name: 'BMI',
  },
  pulse: {
    name: 'Pulse',
  },
  temperature: {
    name: 'Temperature',
    units: 'F',
    longUnit: 'Fahrenheit',
  },
  respiration_rate: {
    name: 'Respiration Rate',
  },
  o2_saturation: {
    name: 'O2 Saturation',
    units: '%',
  },
}

Metadata needs to be sorted on name for each vital in allVitals's taken_on value. So in this example, according to the taken_on time, the weight vital take_on was later than the glucose taken_on. So in the metadata sort where weight would be before glucose. The allVitals array can have more than just two vitals, it could have vitals of all types and multiple of them (i.e, multiple pulse vitals, multiple weight vitals)

const allVitals = 
[
    {
        "patient_id": 79,
        "vital_type_id": 4,
        "value": "171",
        "taken_on": "2022-11-17T13:19:00.000-06:00",
        "vital_type": {
            "id": 4,
            "name": "weight",
            "units": "pounds",
            "created_at": "2022-11-17T13:52:00.375-06:00",
            "updated_at": "2022-11-17T13:52:00.375-06:00"
        },
        "notes": null,
        "source": "patient_device",
        "id": 1399,
        "time_recorded": true,
        "severity": null,
        "formatted_severity": "-",
        "vital_attributes": {},
        "vital_status": "valid"
    },
    {
        "patient_id": 79,
        "vital_type_id": 6,
        "value": "9.76",
        "taken_on": "2022-11-17T11:07:00.000-06:00",
        "vital_type": {
            "id": 6,
            "name": "glucose",
            "units": "mg/dL",
            "created_at": "2022-11-17T13:52:00.360-06:00",
            "updated_at": "2022-11-17T13:52:00.360-06:00"
        },
        "notes": null,
        "source": "patient_device",
        "id": 1366,
        "time_recorded": true,
        "severity": "critical_low",
        "formatted_severity": "Critical - Low",
        "vital_attributes": {},
        "vital_status": "valid"
    }
]

The Expected result would be

const metadata = {
  weight: {
    name: 'Weight',
    units: 'lbs',
    longUnit: 'Pounds',
  },
  glucose: {
    name: 'Glucose',
    units: 'mg/dL',
  },
  height: {
    name: 'Height',
    units: '"',
    longUnit: 'Inches',
  },
  bmi: {
    name: 'BMI',
  },
  pulse: {
    name: 'Pulse',
  },
  temperature: {
    name: 'Temperature',
    units: 'F',
    longUnit: 'Fahrenheit',
  },
  respiration_rate: {
    name: 'Respiration Rate',
  },
  o2_saturation: {
    name: 'O2 Saturation',
    units: '%',
  },
}

As this shows that since weight was taken_on later than glucose it comes first in metadata object

CodePudding user response:

So I solved it with the help of @This Guy so I gave him an upvote

const OrderedVitalsArray = allVitals
    .sort((v1, v2) => moment(v2.taken_on).diff(moment(v1.taken_on)))
    .map((e) => {
      return e?.vital_type?.name
    })

  const OrderedMetadata = OrderedVitalsArray.reduce((current, item) => {
    current[item] = metadata[item]
    return current
  }, {})

CodePudding user response:

Build a list of order, then loop through that, use the items as a key against the original list and rebuild the order.

const metadata = {
  glucose: {
    name: 'Glucose',
    units: 'mg/dL',
  },
  height: {
    name: 'Height',
    units: '"',
    longUnit: 'Inches',
  },
  weight: {
    name: 'Weight',
    units: 'lbs',
    longUnit: 'Pounds',
  },
  bmi: {
    name: 'BMI',
  },
  pulse: {
    name: 'Pulse',
  },
  temperature: {
    name: 'Temperature',
    units: 'F',
    longUnit: 'Fahrenheit',
  },
  respiration_rate: {
    name: 'Respiration Rate',
  },
  o2_saturation: {
    name: 'O2 Saturation',
    units: '%',
  },
}

const allVitals = 
[
    {
        "patient_id": 79,
        "vital_type_id": 4,
        "value": "171",
        "taken_on": "2022-11-17T13:19:00.000-06:00",
        "vital_type": {
            "id": 4,
            "name": "weight",
            "units": "pounds",
            "created_at": "2022-11-17T13:52:00.375-06:00",
            "updated_at": "2022-11-17T13:52:00.375-06:00"
        },
        "notes": null,
        "source": "patient_device",
        "id": 1399,
        "time_recorded": true,
        "severity": null,
        "formatted_severity": "-",
        "vital_attributes": {},
        "vital_status": "valid"
    },
    {
        "patient_id": 79,
        "vital_type_id": 6,
        "value": "9.76",
        "taken_on": "2022-11-17T11:07:00.000-06:00",
        "vital_type": {
            "id": 6,
            "name": "glucose",
            "units": "mg/dL",
            "created_at": "2022-11-17T13:52:00.360-06:00",
            "updated_at": "2022-11-17T13:52:00.360-06:00"
        },
        "notes": null,
        "source": "patient_device",
        "id": 1366,
        "time_recorded": true,
        "severity": "critical_low",
        "formatted_severity": "Critical - Low",
        "vital_attributes": {},
        "vital_status": "valid"
    }
];

const conditionedVitals = allVitals.sort( (a, b) => {
  // get ref to a and b taken_on values
  var a_taken_on = a['taken_on'];
  //console.log( Date.parse(a_taken_on) );
  var b_taken_on = b['taken_on'];
  //console.log( Date.parse(b_taken_on) );
  if ( a_taken_on > b_taken_on ) return 1;
  if ( b_taken_on > a_taken_on ) return -1;
  return 0;
});
//we have the original list, now we need to re-sort, by conditionedVitals list
// get the order of the list, by extracting the order of the conditionedVitals:
const orderList = conditionedVitals.map( (e) => { 
console.log(e.vital_type.name);
return e.vital_type.name } ) ;

console.log(orderList);
// we have our sort order now, we can do things:
const newList = orderList.map( (e) => {
 return metadata[e];
});
// new List prints out in the order which you require, but does not include items which were not included in the allVitals list!
console.log(newList);

  • Related