[英]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 = {}
length
property. length
属性。 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.