计算字符在字符串中出现的次数并将其存储在数组JavaScript中

时间:2022-05-10 23:56:36

I've been trying to figure out how to count how many times a character happens in a string and store it in another variable that will hold the character and the number of times it occurs in the string.

我一直试图弄清楚如何计算一个字符在字符串中出现的次数,并将其存储在另一个变量中,该变量将保存字符及其在字符串中出现的次数。

For example:

var greeting = "Hello World";

[H] occurs [1] time.

[H]发生[1]时间。

[e] occurs [1] time.

[e]发生[1]时间。

[l] occurs [3] times.

[l]发生[3]次。

[o] occurs [2] times.

[o]发生[2]次。

[W] occurs [1] time.

[W]发生[1]时间。

[r] occurs [1] time.

[r]发生[1]时间。

[d] occurs [1] time.

[d]发生[1]时间。

I am a JS Beginner and I tried as much as I can following guides and tutorials but this exercise seems to be out of my league. I would appreciate some help as to how would you guys go on about solving this problem.

我是一个JS初学者,我尽可能多地遵循指南和教程,但这个练习似乎超出了我的联盟。关于你们如何继续解决这个问题,我将不胜感激。

Thanks!

2 个解决方案

#1


0  

You basically want to create a mapped set of characters to it's count in the string. Storing this stuff in an array might be weird as you'd need 2 Dimentional arrays. Instead store it in an hash object.

你基本上想要在字符串中创建一个映射的字符集。将这些东西存储在一个数组中可能很奇怪,因为你需要2个Dimentional数组。而是将其存储在哈希对象中。

var greeting = "Hello world!";

var hash = {};
for(var i = 0; i < greeting.length; i++){
  if(hash[greeting[i]] === undefined){
    hash[greeting[i]] = 1;
  } else {
    hash[greeting[i]] += 1;
  }
}

// printing the stuff in hash.
for(var x in hash){
  if(hash.hasOwnProperty(x)){
    console.log(x, hash[x]);
  }
}

Anyway if you need this stuff in array, you can put so:

无论如何,如果你需要这些东西在数组中,你可以这样:

var arr = [];
var i = 0;
for(var x in hash){
  if(hash.hasOwnProperty(x)){
    arr[i++] = [x, hash[x]];
  }
}

for(var i = 0; i< arr.length; i++){
  console.log(arr[i]);
}

But I wouldn't recommend it. You can see redundancy for yourself.

但我不推荐它。你可以看到自己的冗余。

#2


0  

Try this:

var result = {};

Array.prototype.map.call('Hello world!', function(x) {
  if (typeof result[x] == 'undefined') {
    result[x] = 1;
  } else {
    result[x] += 1;    
  }
});
console.log(result);

var result = {};

Array.prototype.map.call('Hello world!', function(x) {
  if (typeof result[x] == 'undefined') {
    result[x] = 1;
  } else {
    result[x] += 1;    
  }
});
console.log(result);

#1


0  

You basically want to create a mapped set of characters to it's count in the string. Storing this stuff in an array might be weird as you'd need 2 Dimentional arrays. Instead store it in an hash object.

你基本上想要在字符串中创建一个映射的字符集。将这些东西存储在一个数组中可能很奇怪,因为你需要2个Dimentional数组。而是将其存储在哈希对象中。

var greeting = "Hello world!";

var hash = {};
for(var i = 0; i < greeting.length; i++){
  if(hash[greeting[i]] === undefined){
    hash[greeting[i]] = 1;
  } else {
    hash[greeting[i]] += 1;
  }
}

// printing the stuff in hash.
for(var x in hash){
  if(hash.hasOwnProperty(x)){
    console.log(x, hash[x]);
  }
}

Anyway if you need this stuff in array, you can put so:

无论如何,如果你需要这些东西在数组中,你可以这样:

var arr = [];
var i = 0;
for(var x in hash){
  if(hash.hasOwnProperty(x)){
    arr[i++] = [x, hash[x]];
  }
}

for(var i = 0; i< arr.length; i++){
  console.log(arr[i]);
}

But I wouldn't recommend it. You can see redundancy for yourself.

但我不推荐它。你可以看到自己的冗余。

#2


0  

Try this:

var result = {};

Array.prototype.map.call('Hello world!', function(x) {
  if (typeof result[x] == 'undefined') {
    result[x] = 1;
  } else {
    result[x] += 1;    
  }
});
console.log(result);

var result = {};

Array.prototype.map.call('Hello world!', function(x) {
  if (typeof result[x] == 'undefined') {
    result[x] = 1;
  } else {
    result[x] += 1;    
  }
});
console.log(result);

相关文章