javascript三元运算符来计算字符串中的字符数
我最近遇到了这个javascript函数来计算某个字符在字符串中出现的次数。 我可以看到它使用.replace()方法替换任何非空白的正则表达式,但我不太明白它被替换成了什么javascript三元运算符来计算字符串中的字符数,javascript,Javascript,我最近遇到了这个javascript函数来计算某个字符在字符串中出现的次数。 我可以看到它使用.replace()方法替换任何非空白的正则表达式,但我不太明白它被替换成了什么 function Char_Counts(str1) { var uchars = {}; str1.replace(/\S/g, function(l) { uchars[l] = (isNaN(uchars[l]) ? 1 : uchars[l] + 1); }); re
function Char_Counts(str1) {
var uchars = {};
str1.replace(/\S/g, function(l) {
uchars[l] = (isNaN(uchars[l]) ? 1 : uchars[l] + 1);
});
return uchars;
}
console.log(Char_Counts("This is a sample string"));
谁能解释一下传递给未命名函数的参数“l”是什么,以及三元运算符内部到底发生了什么,我设法实现了与此相同的效果,但使用了嵌套for循环,但我甚至看不出这是如何迭代字符串字符的。这是控制台中的输出,我只是想确切地了解发生了什么
Object { T: 1, h: 1, i: 3, s: 4, a: 2, m: 1, p: 1, l: 1, e: 1, t: 1, 3 more… }
像这样做是不寻常的。实际上,这种模式更常用。它获取
uchars[l]
或0
的真实值并添加一个
uchars[l] = (uchars[l] || 0) + 1;
像这样做是不寻常的。实际上,这种模式更常用。它获取
uchars[l]
或0
的真实值并添加一个
uchars[l] = (uchars[l] || 0) + 1;
那么这个函数是怎么回事
function Char_Counts(str1) {
//Create an object where we will hold the letters and thier counts
var uchars = {};
// Get all non space characters matched with /\S/g regex
str1.replace(/\S/g, function (l) {
// here l represents each character as the regex matches it
// so finally the ternary operator assings to the letter property of our map: if we find the letter for the first time isNaN will return true from isNan(undefined) and we will assing 1 otherwise we will increase the count by 1
uchars[l] = (isNaN(uchars[l]) ? 1 : uchars[l] + 1);
});
return uchars;
}
因此,对于在字符串这些
中运行的示例,正则表达式将匹配任何非空格字符,因此这些
,函数中的l
将是T
然后h
然后e
等等
uchars['T']
是未定义的
soisNaN(未定义)
给出true
因此我们将uchars['T']=1代码>
如果第一条语句为true,三元运算符将在?
之后返回计算表达式,否则将在之后返回计算表达式:
从
状况?expr1:expr2
如果条件为true,则运算符返回expr1的值;
否则,它将返回expr2的值
另一种方法是使用Array#reduce
function Char_Counts(str){
return str.replace(/\s/g, "").split("").reduce(function(prev, current){
if(!(current in prev)){ prev[current] = 1} else { prev[current]++}
return prev;
}, {})
}
那么这个函数是怎么回事
function Char_Counts(str1) {
//Create an object where we will hold the letters and thier counts
var uchars = {};
// Get all non space characters matched with /\S/g regex
str1.replace(/\S/g, function (l) {
// here l represents each character as the regex matches it
// so finally the ternary operator assings to the letter property of our map: if we find the letter for the first time isNaN will return true from isNan(undefined) and we will assing 1 otherwise we will increase the count by 1
uchars[l] = (isNaN(uchars[l]) ? 1 : uchars[l] + 1);
});
return uchars;
}
因此,对于在字符串这些
中运行的示例,正则表达式将匹配任何非空格字符,因此这些
,函数中的l
将是T
然后h
然后e
等等
uchars['T']
是未定义的
soisNaN(未定义)
给出true
因此我们将uchars['T']=1代码>
如果第一条语句为true,三元运算符将在?
之后返回计算表达式,否则将在之后返回计算表达式:
从
状况?expr1:expr2
如果条件为true,则运算符返回expr1的值;
否则,它将返回expr2的值
另一种方法是使用Array#reduce
function Char_Counts(str){
return str.replace(/\s/g, "").split("").reduce(function(prev, current){
if(!(current in prev)){ prev[current] = 1} else { prev[current]++}
return prev;
}, {})
}
这是字符串中匹配的字符。。。由于您使用的是\S
每个非空格字符,因此三元字符将检查该字符是否已存在于对象中,如果已存在,则增量计数器将设置为1,这是字符串中匹配的字符。。。由于您使用的是\S
每个非空格字符,因此三元字符检查对象中是否已存在该字符,如果是,则递增计数器设置为1