简体   繁体   English

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

[英]How to sort an object by its values with multiple keys?

I want to use the sort method explained here but I have 我想使用这里说明的排序方法,但是我有

{
  "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,
}

I have created this function where I group every string together and add a count (as the value for the key, see above) 我创建了这个函数,将每个字符串分组在一起并添加一个计数(作为键的值,请参见上文)

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

I have tried to google and look here on Stackoverflow, but have not found any where to sort by the value when the key is not unique . 我试图谷歌,并在这里在Stackoverflow上查看,但是当键不是唯一键时,找不到在哪里按值排序。 Any clue how to solve? 有什么线索怎么解决?

I want this as a result: 我想要这样的结果:

{
  "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
}

Unsing Object.keys() and map as follows you can achieve your expected result. 如下取消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); 

Object.entries and a forEach since map is not the most relevant method to copy key-values Object.entries和forEach,因为map不是复制键值最相关的方法

and as epascarello mentioned, you are better off with an actual array since you should not rely on key order in an object 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); 

Te first thing you'll need to do is turn your objects properties into an array, something in the form 首先,您需要将对象属性转换为数组,形式如下

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

Then you can use sort. 然后,您可以使用排序。

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

As explained in the doc you linked, the sort function accepts an argument, that is the compareFunction. 如您链接的文档中所述, sort函数接受一个参数,即compareFunction。 So, if you want to sort an array of object by their INSTALLATION key, you can simply do: 因此,如果要按对象的INSTALLATION键对对象数组进行排序,则只需执行以下操作:

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

Combine Object.keys(obj) , .sort() & .map 组合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) 

Im sure there is a more elegant solution, but this works too. 我肯定有一个更优雅的解决方案,但这也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM