简体   繁体   English

如何计算JavaScript数组中的字符?

[英]How can I count characters in an JavaScript array?

I need to count the characters from a to z in an array. 我需要计算数组中从a到z的字符。

For example I have an array like this: 例如,我有一个像这样的数组:

["max","mona"]

The desired result would be something like this: 期望的结果将是这样的:

a=2, m=2, n=1, o=1, x=1 a = 2,m = 2,n = 1,o = 1,x = 1

It would be great, if anyone could help me :) 如果有人可以帮助我,那就太好了:)

You can use two forEach loops and return object 您可以使用两个forEach循环并返回对象

 var ar = ["max", "mona"], o = {} ar.forEach(function(w) { w.split('').forEach(function(e) { return o[e] = (o[e] || 0) + 1; }); }); console.log(o) 

Or with ES6 you can use arrow function 或者使用ES6,您可以使用箭头功能

 var ar = ["max","mona"], o = {} ar.forEach(w => w.split('').forEach(e => o[e] = (o[e] || 0)+1)); console.log(o) 

As @Alex.S suggested you can first use join() to return string, then split() to return array and then you can also use reduce() and return object. 正如@ Alex.S建议的那样,您可以首先使用join()返回字符串,然后使用split()返回数组,然后还可以使用reduce()和return object。

 var ar = ["max", "mona"]; var result = ar.join('').split('').reduce(function(o, e) { return o[e] = (o[e] || 0) + 1, o }, {}); console.log(result) 

You can use just one forEach loop and return object 您只能使用一个forEach循环并返回对象

var ar = [ "bonjour", "coucou"], map = {};
ar.join("").split("").forEach(e => map[e] = (map[e] || 0)+1);
console.log(map);

Live Demo 现场演示

https://repl.it/C17p https://repl.it/C17p

I would do it like this; 我会这样做;

 var a = ["max","mona"], charCount = a.reduce((p,w) => w.split("").reduce((t,c) => (t[c] ? t[c]++: t[c] = 1,t),p),{}); console.log(charCount); 

public static void main (String[] args) throws java.lang.Exception
{
    String[] original = {"The","Quick","Brown","Fox","Jumps","Over","The","Lazy","Dog"};
    String singleString ="";
    for(String str : original )
    {
        singleString += str;
    }
     System.out.println(singleString);
    char[] chars = singleString.toLowerCase().toCharArray();
    Arrays.sort(chars);
    String result="";

    for(int i=0;i<chars.length;)
    {
    result += chars[i]+"=";
        int count=0;
        do {
            count++;
            i++;
            } while (i<chars.length-1 && chars[i-1]==chars[i]);

        result += Integer.toString(count)+",";

    }
    System.out.println(result.substring(0,result.length()-1));
}

The solution using Array.join , Array.sort and String.split functions: 使用Array.joinArray.sortString.split函数的解决方案:

var arr = ["max","mona"],
    counts = {};

arr = arr.join("").split(""); // transforms the initial array into array of single characters
arr.sort();
arr.forEach((v) => (counts[v] = (counts[v])? ++counts[v] : 1));

console.log(counts);  // {a: 2, m: 2, n: 1, o: 1, x: 1}

Try this: 尝试这个:

var words = ['max', 'mona'],
    output = {};
    words.forEach(function(word){ 
    for(i=0; i < word.split('').length; i++){
    if(output[word[i]])
      output[word[i]] += 1;
    else{
      output[word[i]] = 1;
    }  
  } 
});

ps: sorry for the code not being formatted, I'm still getting used to the editor =) ps:对不起,代码未格式化,我仍然习惯于编辑器=)

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

相关问题 如何计算JavaScript字符串末尾的换行符数量? - How can I count the number of newline characters at the end of a string in javascript? 如何在 javascript 的数组中放置特殊字符 - How can i place special characters in an array in javascript 如何使用 javascript 计算字符串数组中单词的出现次数? - How can I count the occurrences of a word in an array of strings using javascript? 如何在JavaScript中计算数组数组中出现的次数 - How can I count the number of occurrences in an array of arrays in JavaScript 如何删除和计算对象数组中的重复项? - Javascript - How can I remove and count duplicates in an array of objects? - Javascript 我如何计算 Javascript 中多维数组中的相似值 - How can i count the similar values in a Multidimensional Array in Javascript 如何计算文本区域中的字符? - How can I count the characters from a textarea? 如何根据数组的字符串元素的第一个字符在Javascript中对数组进行排序? -JavaScript - How can i sort array in Javascript based on the first characters of the string element of the array ? - javascript 使用javascript,我如何计算亚洲字符和英语单词的混合 - using javascript, how can I count a mix of asian characters and english words 如何计算 JS (javascript) 中字符串中的字符数(来自数组) - How to count the amount of characters (from an array) in a string in JS (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM