1

I have information coming in as a Map in Javascript.

I want to sort it based on types I have set as an enum.

The data looks like this:

Map(5)

{"Adopted" => 105, "Pending" => 176, "Missing" => 3, "InterestedParties" => 45, "WaitingForPickup" => 15}

Though it may make more sense when seen this way:

new Map<string, number>([
  ['Adopted', 105],
  ['Pending', 176],
  ['Missing', 3],
  ['InterestedParties', 45],
  ['WaitingForPickup', 15],
]);

I want to sort this data into categories like "unadopted animals", "homed animals", "Unknown status", so I made a group of data that map to enum types (so AnimalStatusType.Pending is "Pending")

  const metricGroups = {
    unAdopted: [
      AnimalStatusType.Pending,
      AnimalStatusType.InterestedParties,
    ],
    adopted: [
      AnimalStatusType.Adopted,
    ],
    unknown: [
      AnimalStatusType.Missing,
      AnimalStatusType.WaitingForPickup
    ],
  }

I want to end up with my Map sorted by the three different categories adopted, unadopted, and unknown to look something like this:

{
  adopted: [['adopted', 105]],
  unadopted: [['pending', 176],['interestedParties', 45]],
  unknown: [ ['missing', 3],['waitingForPickup', 15]],
}

but I'm having a bit of difficulty finding anything that does the mapping properly. I think maybe combining Array.from or a reduce functionality can work, but searching mostly pulls up Javascript's .map functionality which is not helping.

 const GroupedData =  Array.from(animalData).reduce((acc, metric) => {
        acc[metricGroups[metric]].push(metric)
      })

3 Answers 3

1

You can do this with map and Object.entries/Object.fromEntries

const input = new Map([
  ['Adopted', 105],
  ['Pending', 176],
  ['Missing', 3],
  ['InterestedParties', 45],
  ['WaitingForPickup', 15],
]);

const metricGroups = {
  unAdopted: [
    "Pending",
    "InterestedParties",
  ],
  adopted: [
    "Adopted",
  ],
  unknown: [
    "Missing",
    "WaitingForPickup"
  ],
}

const result = Object.fromEntries(Object.entries(metricGroups).map(([key, values]) => {
  return [
    key,
    values.map(value => ([value, input.get(value)]))
  ]
}));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

You could just simply reduce your metricGroups object and map the enum values to the appropriate key-value pair in the input.

Note that you cannot sort an object's keys.

const AnimalStatusType = {
  Adopted: 'adopted',
  InterestedParties: 'interestedParties',
  Missing: 'missing',
  Pending: 'pending',
  WaitingForPickup: 'waitingForPickup'
};

const metricGroups = {
  unAdopted: [
    AnimalStatusType.Pending,
    AnimalStatusType.InterestedParties,
  ],
  adopted: [
    AnimalStatusType.Adopted,
  ],
  unknown: [
    AnimalStatusType.Missing,
    AnimalStatusType.WaitingForPickup
  ],
};

const input = { 'Adopted': 105, 'Pending': 176, 'Missing': 3, 'InterestedParties': 45, 'WaitingForPickup': 15 };

const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1);

const result = Object.entries(metricGroups)
  .reduce((acc, [key, value]) => ({
    ...acc,
    [key]: value.map(v => [v, input[capitalize(v)]])
  }), {});

console.log(result);
.as-console-wrapper { top: 0; max-height: 100% !important; }

Comments

0
//the sortMe function sorts the list of maps by fieldName in acs/desc order 

function getASCOrder(){ // <-- create sample list map to be sorted
    var retval = new java.util.ArrayList();
    for (var i = 0; i<5 ; i++){
        var map = new java.util.HashMap();
        map.put("channelId", "3"+ i);
        map.put("some_data", "data"+ i);
        retval.add(map);
    }
    return retval;
}
 
var data = this.getASCOrder(); //<-- get sample data
data = [{channelId=30, some_data=data0}, {channelId=31, some_data=data1}, {channelId=32, some_data=data2}, {channelId=33, some_data=data3}, {channelId=34, some_data=data4}]

var sorted_array  = sortMe(data, "channelId", false); // <-- sort data in reverse order

sorted_array = [{channelId=34, some_data=data4},{channelId=33, some_data=data3},{channelId=32, some_data=data2},{channelId=31, some_data=data1}, {channelId=30, some_data=data0}]

function sortMe(arrayList, sortFieldName, asc) {

   var icount = arrayList.size();
   var temp = null;
   for (var i = 0; i < icount; i++) {
      var n = i + 1;
      if (n < icount) {
         var curMap = arrayList.get(i);
         var nextMap = arrayList.get(n);
        if(asc){ 
             if(curMap.get(sortFieldName) > nextMap.get(sortFieldName)) {
                arrayList.remove(n);
                arrayList.add(i, nextMap);
                sortMe(arrayList, sortFieldName, asc); // <-- recursive call here
             }
         } else { 
             if (curMap.get(sortFieldName) < nextMap.get(sortFieldName)) {
                arrayList.remove(n);
                arrayList.add(i, nextMap);
                sortMe(arrayList, sortFieldName, asc);
             }        
         } 
      }
   }
   return arrayList;
}

1 Comment

new java.util.ArrayList and new java.util.HashMap? Are you sure you wanted to answer this question?

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.