简体   繁体   English

如何在字符串中找到2个最常见的字母并使用javascript返回一个新字符串

[英]How do I find the 2 most frequent letters in a string and return a new string with them with javascript

I'm working on a problem that ask to take a string string and return a new string with the 2 most frequent letters in the string in alternating order. 我正在上要求带一个字符串的问题string并在返回与2个最常见的字母一个新的字符串string交替排列。

my code: 我的代码:

var word = "pponoomababaaa";
var len = word.length;

function twoLetters (word){
var arr = []
word = word.split("")
for (var i = 0; i < word.length; i++){
    if ((arr[word[i]])) arr[word[i]] += 1
    else arr[word[i]] = 1
  }
return arr  // [ p: 2, o: 3, n: 1, m: 1, a: 5, b: 2 ]
}
twoLetters(word)

This returned arr has a length of 0, which I don't understand, so I can't really do anything with it. 这个返回的arr的长度为0,我不理解,所以我真的不能做任何事情。 I have tried putting it into an object but it still has the same issue. 我尝试将其放入对象中,但仍然存在相同的问题。 How do get arr to equal a key:value pair and then find the 2 highest values and them add the keys to a string in alternating order. 如何使arr等于key:value对,然后找到2个最高values ,它们将keys以交替顺序添加到字符串中。 Am I going in the right direction with the code I have? 我使用的代码朝正确的方向前进吗?

It works if you change your array to an object: 如果将数组更改为对象,则可以使用:

var word = "pponoomababaaa";
var len = word.length;

function twoLetters (word){
  var arr = {}
  word = word.split("")
  for (var i = 0; i < word.length; i++){
    if ((arr[word[i]])) arr[word[i]] += 1
    else arr[word[i]] = 1
  }
  return arr  // { p: 2, o: 3, n: 1, m: 1, a: 5, b: 2 }
}

twoLetters(word)

In JavaScript, arrays ( [] ) don't contain key-value pairs (well, sort of: the indexes are keys so-to-speak). 在JavaScript中,数组( [] )不包含键值对(嗯,有点:索引是可以说的键)。 The data structure you're looking for is a plain object ( {} ), which can hold key-value pairs. 您要查找的数据结构是一个普通对象( {} ),它可以保存键值对。

Edit 编辑

Just to add a bit of clarification, you can add members onto an array ( [] ), but your array length will be 0. Here's the output from my browser console in Chrome, where you can see that you can add members onto the array itself. 只是为了澄清一下,您可以将成员添加到数组( [] ),但是您的数组长度将为0。这是我在Chrome浏览器控制台中的输出,您可以在其中看到可以将成员添加到数组中本身。 However this approach isn't recommended. 但是,不建议使用此方法。

在此处输入图片说明

You can use Map for this 您可以为此使用地图

 var myMap = new Map(); var word = "pponoomababaaa"; var chars = word.split(""); chars.forEach(function(char) { if (!myMap.get(char)) { myMap.set(char, 1); } else { var count=myMap.get(char); count+=1; myMap.set(char,count); } }); for (var [key, value] of myMap.entries()) { console.log(key + " = " + value); } //if to copy to new array var newarr=Array.from(myMap); console.log(newarr); 

Hope this helps 希望这可以帮助

var word = "pponoomababaaa";
var len = word.length;

function twoLetters (word){
var arr = []
word = word.split("")
for (var i = 0; i < word.length; i++){
    if ((arr[word[i]])) arr[word[i]] += 1
    else arr[word[i]] = 1
  }
console.log(arr.p)
return arr  // [ p: 2, o: 3, n: 1, m: 1, a: 5, b: 2 ]

}
twoLetters(word)

You're not creating an array, you're creating an object. 您不是在创建数组,而是在创建对象。 It does have a key:value pair, as you'll see in the console.log(arr.p) For it to be an array, you would need to push into arr. 它确实具有key:value对,如您将在console.log(arr.p)看到的那样,要使其成为数组,您需要将其push入arr。

Here is your updated code with arr as an object( {} ) not an array( [] ) 这是更新的代码,其中arr作为对象( {} )而不是数组( []

 var word = "pponoomababaaa"; var len = word.length; function twoLetters (word){ var arr = {}; word = word.split("") for (var i = 0; i < word.length; i++){ if ((arr[word[i]])) arr[word[i]] += 1 else arr[word[i]] = 1 } return arr; // [ p: 2, o: 3, n: 1, m: 1, a: 5, b: 2 ] } console.log(twoLetters(word)) 

Setting keys in array would turn it into an object. 在数组中设置键将把它变成一个对象。 Your arr = [] was working fine in jsbin( http://jsbin.com/povimehiyu/)(check the console). 您的arr = []在jsbin( http://jsbin.com/povimehiyu/ )中运行正常(请检查控制台)。 But not here in code snippet, hence modified arr = {} 但不在代码段中,因此修改了arr = {}

  • Adding key-value in object won't create/update the length property. 在对象中添加键值将不会创建/更新length属性。
  • In your code above, adding key in arr changed it into an object, and length property was not updated whenever who were setting a key-value in your arr 在上面的代码中,在arr添加键将其更改为对象,并且只要在arr中设置键值的人就不会更新length属性。

暂无
暂无

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

相关问题 Javascript获取字符串中所有最常用的字符 - Javascript get all the most frequent characters in a string 查找字符串中出现频率最高的字符 javascript - Finding the most frequent character in a string javascript 如何在javascript中返回带有对象的字符串? - How do I return a string with an object in javascript? 如何从javascript中的字符串中删除字母并返回字符串的其余部分? - How can I remove letters from a string in javascript and return the remainder of the string? 如何在Javascript中将随机字母字符串分成几对子字符串并用新字符连续替换? - How do you divide a string of random letters into pairs of substrings and replace with new characters continuously in Javascript? 如何使用Javascript或jQuery在网页上找到最常用的类? - How to find most frequent used class on a web page with Javascript or jQuery? 如何在.txt文件中查找/定位最常见的单词并进行更改 - How do I find/target the most frequent word in .txt file and change it 如何获取数组中出现频率最高的项目(数字或字符串)? - How to get the most frequent item (number or string) in an array? 如何在gethash javascript function中查找带有特定字母的字符串 - How to find a string with specific letters in gethash javascript function 如何确定字符串是否只有字母、数字和下划线字符? - How do I find if a string has only letters, numbers and underscore characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM