如何根据 Javascript 中的值对数组的映射数组进行排序

问题描述 投票:0回答:3

我有信息作为 Javascript 中的地图传入。

我想根据我设置为枚举的类型对其进行排序。

数据是这样的:

地图(5)

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

尽管这样看可能更有意义:

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

我想将这些数据分类为“未收养的动物”、“家庭动物”、“未知状态”等类别,所以我制作了一组映射到枚举类型的数据(所以

AnimalStatusType.Pending
是“待定”)

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

我想把我的地图按已采用、未采用和未知的三个不同类别排序,看起来像这样:

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

但是我很难找到任何可以正确映射的东西。我认为也许结合

Array.from
或减少功能可以工作,但搜索主要是拉起 Javascript 的 .map 功能,这无济于事。

 const GroupedData =  Array.from(animalData).reduce((acc, metric) => {
        acc[metricGroups[metric]].push(metric)
      })
javascript arrays sorting multidimensional-array reduce
3个回答
1
投票

您可以使用

map
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);


1
投票

您可以简单地减少您的

metricGroups
对象并将枚举值映射到
input
中的适当键值对。

请注意,您无法对对象的键进行排序。

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; }


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

function getASCOrder(){ // <-- create 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}]

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


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;
}

result = [{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}]

© www.soinside.com 2019 - 2024. All rights reserved.