简体   繁体   English

如何以数字方式对具有多个参数的对象数组进行排序

[英]How to sort a object array with more than one argument numerically

I need to create a function in my web application.我需要在我的 Web 应用程序中创建一个函数。 This function receives as parameter a JS object and returns the organized list from lowest to highest.该函数接收一个 JS 对象作为参数,并从低到高返回组织好的列表。 The object has two parameters to analyze.该对象有两个要分析的参数。

var medicines = [
  {type:"Glibenclamida 5 mg", hour1:2, hour2: 4},
  {type:"Noretisterona 0,35 mg", hour1:4, hour2: 8},
  {type:"Glibenclamida 99 mg", hour1:8, hour2: 16}
];

So, this is only an example and I need these list return...所以,这只是一个例子,我需要这些列表返回......

1: hour 2- Glibenclamida 5 mg
2: hour 4- Glibenclamida 5 mg, Noretisterona 0,35 mg
3: hour 8- Noretisterona 0,35 mg, Glibenclamida 99 mg
4: hour 16 - Glibenclamida 99 mg

This is just an example I need an organized list like these.这只是一个例子,我需要一个像这样有组织的列表。

This could be solved using reduce, have a look at the solution below.这可以使用reduce来解决,看看下面的解决方案。

 var medicines = [ {type:"Glibenclamida 5 mg", hour1:2, hour2: 4}, {type:"Noretisterona 0,35 mg", hour1:4, hour2: 8}, {type:"Glibenclamida 99 mg", hour1:8, hour2: 16} ]; var convertedMedicines = medicines.reduce((res, medicine) => { res[medicine.hour1] = res[medicine.hour1] || []; res[medicine.hour2] = res[medicine.hour2] || []; res[medicine.hour1].push(medicine.type); res[medicine.hour2].push(medicine.type); return res; }, {}); console.log(convertedMedicines)

This can also be done via the map function.这也可以通过 map 函数来完成。 Map the array of objects and then check if the index of type exists.映射对象数组,然后检查类型的索引是否存在。 If it does then we push it normally, if it does not exist then we push it with the previous value (iln -1) .如果是,那么我们正常推送它,如果它不存在,那么我们用之前的值(iln -1)推送它。

 var medicines = [{ type: "Glibenclamida 5 mg", hour1: 2, hour2: 4 }, { type: "Noretisterona 0,35 mg", hour1: 4, hour2: 8 }, { type: "Glibenclamida 99 mg", hour1: 8, hour2: 16 } ]; let an = medicines.map((val, iln) => { if (!iln && medicines[iln + 1].type) { return { [iln + 1]: "hour" + val.hour1 + "-" + val.type } } else { return { [iln + 1]: "hour" + medicines[iln].hour1 + "-" + medicines[iln - 1].type + "," + medicines[iln].type } } }) an.push({[medicines.length + 1]: "hour" + medicines[medicines.length-1].hour2 + "-" + medicines[medicines.length-1].type}) console.log(JSON.stringify(an))

We push the last value manually because it won't have a successor and doing it inside map will be useless.我们手动推送最后一个值,因为它没有后继,并且在 map 中执行它是无用的。

You get the output as你得到的输出为

[{
  "1": "hour2-Glibenclamida 5 mg"
}, {
  "2": "hour4-Glibenclamida 5 mg,Noretisterona 0,35 mg"
}, {
  "3": "hour8-Noretisterona 0,35 mg,Glibenclamida 99 mg"
}, {
  "4": "hour16-Glibenclamida 99 mg"
}]

Improvement on Marius' reduce改进马里乌斯的减少

This will not hardcode hour1 and hour2 - in case there will be an hour3 etc - So any key beginning with hour is used这不会对 hour1 和 hour2 进行硬编码 - 以防有 hour3 等 - 因此使用以小时开头的任何键

 var medicines = [ {type:"Glibenclamida 5 mg", hour1: 2, hour2: 4}, {type:"Noretisterona 0,35 mg", hour1: 4, hour2: 8, hour3: 16}, {type:"Glibenclamida 99 mg", hour1: 8, hour2: 16} ]; var convertedMedicines = medicines.reduce((res, medicine) => { Object.keys(medicine).forEach(key => { if (key.indexOf("hour")==0) { res["hour "+medicine[key]] = res["hour "+medicine[key]] || []; res["hour "+medicine[key]].push(medicine.type); } }); return res; }, {}); console.log(convertedMedicines)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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