简体   繁体   中英

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

I need to create a function in my web application. This function receives as parameter a JS object and returns the organized list from lowest to highest. 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.

 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 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) .

 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.

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

 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)

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