0

Working with arrays is definitely one of my weakest area, so any help is greatly appreciated. 😊

To add to the challange, this is for a WebOS application that has the following limitations ...

  • JavaScript 5.0
  • Only extra library is JQuery
  • No arrow functions (=>)

Example of received array ...

var Schedule = [{
  "category": "Laboratory", "status": "COMPLETE" }, {
  "category": "Radiology" , "status": "COMPLETE" }, {
  "category": "Laboratory", "status": "SCHEDULED"}, {
  "category": "Laboratory", "status": "COMPLETE" }, {
  "category": "Radiology" , "status": "SCHEDULED"}, {
  "category": "Laboratory", "status": "COMPLETE" }, {
  "category": "Doppler"   , "status": "SCHEDULED"
}]

Desired conversion ...

var ScheduleFormatted = [{
  "category": "Laboratory", "complete": "3", "total": "4" }, {
  "category": "Radiology" , "complete": "1", "total": "2" }, {
  "category": "Doppler"   , "complete": "1", "total": "1" }, {
}]

It would be especially great to have the incomplete categories listed first.

I've been successful in achieving parts of this (like getting unique properties, or instance count by status), but the complexity of this has me completely stumped.

Please help.

4
  • please add the part which is working. Commented Apr 17, 2020 at 16:33
  • @NinaScholz -- What success I've had has been limited, but you're welcome to take a look: jsfiddle.net/MangaX/d7vx5a93 Commented Apr 18, 2020 at 21:03
  • Please edit that into the question, then it's worth reopening. Also you can ping me here with @Jonas ... but digging up my email also works :) Commented Apr 18, 2020 at 21:51
  • @JonasWilms -- LOL! Sorry about that. Appreciate you taking a second look, and especially for the help. Commented Apr 20, 2020 at 10:44

1 Answer 1

1

You could create lookup objects where you store the total / completed count for each category:

 var total = {}, complete = {};

 Schedule.forEach(function(item) {
    var c = item.category;
    total[c] = (total[c] || 0) + 1;
    if(item.status === "COMPLETED") 
      complete[c] = (complete[c] || 0) + 1;
 });

 var ScheduleFormatted = Object.keys(total).map(function(category) {
    return { 
     category: category, 
     total: "" + total[category],
     complete: "" + (complete[category] || 0)
   };
 });
Sign up to request clarification or add additional context in comments.

2 Comments

That is an absolutely perfect solution! Thank you good sir. :) The only change I made was to add the following check before the return ... if (complete[category] == undefined) { complete[category] = 0; } ... Appreciate the help so very much.
I see that you updated the answer to address the "complete" amount returning as "undefined" -- and in a much more elegant way than the "if" statement I included. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.