【问题标题】:How do I sort an object based on an attribute in an array of objects如何根据对象数组中的属性对对象进行排序
【发布时间】:2023-02-03 00:52:09
【问题描述】:

我想在下面对一个名为元数据的对象进行排序:

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: '%',
  },
}

元数据需要根据 allVitals 的 taken_on 值中每个生命体征的名称进行排序。所以在这个例子中,根据 taken_on 时间,weight vital take_on 晚于 glucose taken_on。因此,在元数据排序中,重量将排在葡萄糖之前。 allVitals 数组可以有不止两个生命体征,它可以有所有类型的生命体征和其中的多个生命体征(即,多个脉搏生命体征,多个体重生命体征)

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"
    }
]

【问题讨论】:

  • 顺便说一句,您可以期望 allVitals 总是首先按最新的 vitals 排序
  • 预期结果是什么?

标签: javascript arrays sorting object


【解决方案1】:

可以实现一些额外的快捷代码,但这有助于理解正在发生的事情: 我们将属性解析为 Date 对象并比较值。

参考排序:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

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 newList = 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;
});

console.log(newList);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多