简体   繁体   English

如何对对象进行排序,以使所有值都按升序排列?

[英]How Can I Sort an Object so that all the values are in ascending order?

I am trying to do word frequency and I have something working as far as printing out the word frequency. 我正在尝试进行词频检查,但我有一些方法可以打印出词频。 I now want to sort the output in Ascending Order. 我现在想按升序对输出进行排序。

var paragraph = "Into the bored patent composes the synonymous cheer. The playing essence mothers an offset. Does the alleged cap fast? Why can't the covered fish urge the word? The cyclist works within a laughing jam. When will my smooth year entitle the public?";

un_puncutated_paragraph = paragraph.replace(/[~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,"");
let wordsHolder = un_puncutated_paragraph.split(' ');
let frequencyMap = {};

wordsHolder.forEach((word) => {
  if (!frequencyMap[word]) {
    frequencyMap[word] = 0;
  }
  frequencyMap[word] += 1;
});

Also, could someone explain this logic a bit better, not entirely sure what is going on? 另外,有人可以更好地解释这种逻辑,而不是完全确定发生了什么吗?

  if (!frequencyMap[word]) {
    frequencyMap[word] = 0;
  }
  frequencyMap[word] += 1;

Thanks! 谢谢!

 var paragraph, arrText, getVal, finalString; var objText = [], arrGetText = []; paragraph = "Into the bored patent composes the synonymous cheer. The playing essence mothers an offset. Does the alleged cap fast? Why can't the covered fish urge the word? The cyclist works within a laughing jam. When will my smooth year entitle the public?"; // Step 1. Split Text with space arrText = paragraph.split(" "); // Step 2. Create length and text based object for (var index = 0; index < arrText.length; index++) { objText.push({ len: arrText[index].length, text: arrText[index] }); } // Step 3. Sort object by length objText.sort(function(a, b) { return a.len - b.len; }); // Step 4. Extract value from sorted object and push into a new array for (var index = 0; index < arrText.length; index++) { getVal = Object.values(objText[index]); arrGetText.push(getVal[1]); } // Step 5. Finally join array with with space and produce ascending order sorted string. var finalString = arrGetText.join(" "); console.log(finalString); 

 if (!frequencyMap[word]) { // If word does not exists as key in array...
    frequencyMap[word] = 0; // ...add key in array
  }
 frequencyMap[word] += 1; //  if existing or just created increment word count

To sort your final frequencyMap object by the frequency of each word you can try: 要按每个单词的频率对最终的frequencyMap对象进行排序,可以尝试:

Object.keys(frequencyMap)
    .sort()
    .reduce((a, v) => {
      a[v] = frequencyMap[v];
      return a; }, {});

暂无
暂无

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

相关问题 按key升序对object进行排序 - Sort object by keys in ascending order 如何按具有相同字符串的属性对嵌套的 Object 进行排序,按升序移动值并在 JavaScript 中创建 ID - How to Sort Nested Object By Property With Same String, Move Values in Ascending Order & Create Id in JavaScript 我如何获得 wordpress 插件,以使来自前端的访问者能够按升序或降序对帖子进行排序 - How can i get a wordpress plugin to enable visitors from frontend to sort posts by ascending order or descending order 如何更改排序 function 以同时接受升序和降序 - How can i change my sort function to both accept ascending order and descending order 如何使用排序 function 按名称对对象的元素进行排序并提取值? - How can I use the sort function to sort the object's elements in order by name and extract the values? 如何通过js中的键值以升序对对象进行排序? - How to sort a object in ascending order by the value of a key in js? 如何将标记为“价格”的所有 class 值存储到一个数组中以便对它们进行排序? - How do I store all my class values labeled "price" into an array so I can sort them? 请如何按时间升序排序时间范围? - Please How do I sort range of time in ascending order? 如何按年龄升序对这些人的数据进行排序 - How do I sort this data of people in ascending order of their age JavaScript 根据子 object 值对 object 中的值进行升序或降序排序 - JavaScript sort ascending or descending order values in object base on sub object value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM