简体   繁体   English

如何计算 Javascript 中数组内的字母或字符总数

[英]How to count the total number of letters or characters inside of an array in Javascript

Part of a JavaScript program I am writing asks to count the total number of the letters from the array objects.我正在编写的 JavaScript 程序的一部分要求计算来自数组对象的字母总数。 So for example if I had an array like this.例如,如果我有一个这样的数组。

["apple", "pear"] 

The total count of letters would be 9. I am not sure if their is a char count function in JavaScript and a lot of the examples I have seen on here only count the occurrences of individual letters.字母的总数将是 9。我不确定它们是否是 JavaScript 中的字符数 function 并且我在这里看到的许多示例仅计算单个字母的出现次数。 Any help with this would be appreciated.对此的任何帮助将不胜感激。

You can do this by concatenating the array then getting the length of it.您可以通过连接数组然后获取它的长度来做到这一点。 For example ["apple", "pear"] would become "applepear" then the length of that string which is 9 .例如["apple", "pear"]将变为"applepear"然后该字符串的长度为9

 let array = ["apple", "pear"]; let result = array.join("").length; console.log(result);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM