如何使用多个键按对象的值对对象进行排序?

时间:2019-04-04 15:51:43

标签: javascript arrays sorting object

I want to use the sort method explained here但我有

{
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
}

我创建了这个函数,将每个字符串分组在一起并添加一个计数(作为键的值,请参见上文)

function generateCountDuplication(wordArray) {
  const countedWords = wordArray.reduce(function(allWords, word) {
    if (word in allWords) {
      allWords[word]++;
    }
    else {
      allWords[word] = 1;
    }
    return allWords;
  }, {});
  return countedWords;
}

我尝试过搜索Google,并在Stackoverflow上查看了此内容,但是在键不是唯一的时,找不到任何根据值排序的地方。有什么线索怎么解决?

我想要这个结果:

{
  "TUBE": 1,
  "DCS": 2,
  "BREAKOUT": 6,
  "RAISING": 8,
  "REAR": 61,
  "TOWER": 65,
  "JACKS": 84,
  "FEED": 114,
  "INSTALLATION": 119,
  "ASSEMBLY": 326,
  "JACK": 334,
  "HYDRAULIC": 1421,
  "CYLINDER": 2986
}

5 个答案:

答案 0 :(得分:3)

按以下步骤打开Object.keys()map,可以达到预期的效果。

const list = {
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
};

var newList = {};
Object.keys(list).sort(function(a,b){return list[a]-list[b]})
                 .map(key => newList[key] = list[key]);
console.log(newList);

答案 1 :(得分:2)

Object.entries和forEach,因为map不是复制键值最相关的方法

并且如epascarello所述,最好使用实际数组,因为您不应该依赖对象中的键顺序

const list = {
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
},newList = {}

Object.entries(list).sort((a,b) => a[1]-b[1])
  .forEach(elm => newList[elm[0]] = elm[1])

console.log(newList);

答案 2 :(得分:1)

您需要做的第一件事就是将对象属性转换为数组,形式如下

[
   {word: "CYLINDER", count: 2986},
   {word: "HYDRAULIC", count: 4421}
   // etc
]

然后您可以使用排序。

var input = {
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
};

var result = Object.keys(input)
                   .map( x => ({word:x, count:input[x]}))
                   .sort( (a,b) => a.count - b.count);
console.log(result);

答案 3 :(得分:0)

如您链接的文档中所述,sort函数接受一个参数,即compareFunction。因此,如果您想通过对象的INSTALLATION键对对象数组进行排序,只需执行以下操作:

array.sort(function(a,b) {return a.INSTALLATION - b.INSTALLATION})

答案 4 :(得分:0)

合并Object.keys(obj).sort().map

const obj = {
  "CYLINDER": 2986,
  "HYDRAULIC": 1421,
  "JACKS": 84,
  "INSTALLATION": 119,
  "REAR": 61,
  "JACK": 334,
  "TUBE": 1,
  "FEED": 114,
  "ASSEMBLY": 326,
  "DCS": 2,
  "TOWER": 65,
  "RAISING": 8,
  "BREAKOUT": 6,
}

var sorted = {};

console.log("Un-sorted", obj);

Object.keys(obj).sort().map(function(key){
  sorted[key] = obj[key];
});

console.log("Sorted", sorted)

我肯定有一个更优雅的解决方案,但这也可行。