<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var obj = {}; var str = "helloworld"; str.split('').forEach(function (e, i) { //第一种 if (obj.hasOwnProperty(e)) { obj[e] += 1 } else { obj[e] = 1 } //第二种方法 if (!(e in obj)) { obj[e] = 1 } else { obj[e] += 1 } }); console.log(obj) </script> </body> </html>
判断一个字符串中的某一个字母出现的次数
最新推荐文章于 2022-06-21 20:06:05 发布