【问题标题】:How Can I Sort an Object so that all the values are in ascending order?如何对对象进行排序以使所有值都按升序排列?
【发布时间】:2018-07-18 02:23:12
【问题描述】:

我正在尝试做词频,并且我有一些工作可以打印出词频。我现在想按升序对输出进行排序。

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

另外,有人可以更好地解释这个逻辑,不完全确定发生了什么吗?

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

谢谢!

【问题讨论】:

  • 所以.. 你写了这段代码但不明白你写了什么?或者你从某个地方复制了这段代码并想理解它?数组上有一个sort 方法...
  • 所以如果我没记错的话,你想按长度升序显示更新的段落文本吗?
  • 我写了除了 if 块之外的所有东西。我正在尝试按升序对频率图的输出进行排序。因此,例如 Into 被计算了 6 次,因为它应该在打印 frequencyMap 时位于顶部。
  • 请查看我的回答给我您的反馈。

标签: javascript algorithm sorting


【解决方案1】:

要按照每个单词出现的频率对最终的frequencyMap 对象进行排序,您可以尝试:

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

【讨论】:

  • 您问题中的第二段代码是创建一个计数。要了解发生了什么,您可以阅读此链接medium.freecodecamp.org/reduce-f47a7da511a9 中的Creating a Tally with the Reduce Method In JavaScript 部分(您使用的是forEach,而不是reduce,但对本示例中发生的情况的解释是相同的)。如果您仍然不明白,我建议您打开一个单独的堆栈问题,以免您将这两个问题混为一谈。单独的、有针对性的问题将使其他有相同问题的人在未来变得更容易。
【解决方案2】:
 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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 感谢您的回复。我想我可能把这个措辞错了。我的意思是我需要从 wordsHolder 中取出这些单词,计算每个单词的出现次数,然后按升序显示这些单词。示例输出:2 中的 6 代表 1。等等
    • @DudeRanch 我认为在你的问题中写下了你的实际要求。
    猜你喜欢
    • 2019-07-23
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2020-11-09
    • 2019-01-04
    • 1970-01-01
    相关资源
    最近更新 更多