- 博客(3)
- 收藏
- 关注
原创 js 作用于array的几个常见高阶函数
map()map()函数把每个元素依次作用于传入的函数。如把[1, 2, 3, 4]中的每个元素变成平方,即[1, 4, 9, 16],实现如下:var arr = [1, 2, 3, 4]var result = arr.map(x => x * x)console.log(result) // [1, 4, 9, 16]reduce()reduce()函数把传入的函数连续作用于array的每个元素。如求[1, 2, 3, 4]中所有元素的累积,实现如下:var arr = [1,
2020-07-25 22:55:36
140
原创 js map实现用户名首字母大写
Purpose利用map规范用户名,首字母大写,其他字母小写Map is used to standardize the user name, the first letter is uppercase, and other letters are lower case.Codefunction normalize(username) { return username.map(function(s) { return s[0].toUpperCase() + s.subst
2020-07-25 17:00:14
214
原创 js map+reduce实现string转number
Purpose:不使用JavaScript内置的parseInt()函数,利用map和reduce操作实现一个string2int()函数:Instead of using the parseInt() function built in JavaScript, a string2int() function is implemented by using map and reduce operations.Code:function string2int(s) { return s.spl
2020-07-25 16:30:23
121
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人