Javascript实用方法

 

这篇我主要记录一些在工做中经常使用的、实用的方法。git

String

trim

字符串方法中的trim主要用来去空格使用,不少时候,在后台作参数处理的时候,咱们都会使用该方法,好比在获取用户输入的帐户时github

var a = String(' 1234 ');
var b = "hello world";

console.log(b.trim()); //hello world
console.log(a.trim()); //1234
console.log(a); // 1234

以上,能够看出,trim方法去掉的是两头的空格,对中间的空格并不会产生影响。值得注意的是,trim方法,返回的是一个新的字符串,对原来的变量没有影响。web

slice

slice方法用于切割字符串,用到的地方挺多的,好比在处理文件流的时候,咱们就能够用slice来切割,固然,大多时候,也就仅仅是对字符串作一个简单处理数组

var b = "hello world, I like you";

console.log(b.slice(13)); //I like you
console.log(b.slice(15,20)); //like
console.log(b.slice(15,-4)); //like
console.log(b); //hello world, I like you

slice方法,支持传入两个参数,开始位置与结束位置,传入负数时,表明这次计数从末尾开始。异步

split

split方法用于将字符串分组存放,是经常使用的方法之一。可以让咱们对分组好的字符串进行分组操做ui

var b = "hello world, I like you, and you?";

console.log(b.split()); //[ 'hello world, I like you, and you?' ]
console.log(b.split('')); //["h","e","l","l","o"," ","w","o","r","l","d",","," ","I",
" ","l","i","k","e"," ","y","o","u",","," ","a","n","d"," ","y","o","u","?"]
console.log(b.split(' ')); //[ 'hello', 'world,', 'I', 'like', 'you,', 'and', 'you?' ]
console.log(b.split(' ',3)); //[ 'hello', 'world,', 'I' ]

以空字符分组的时候,会将字符串按每个字符来分组,这点很实用,能够按字符分组,想一想都会以为程序的世界也是鸟语花香啊~spa

substr、substring

都是用来截取字符串的,substr方法,支持传入两个参数,一个是开始位置,一个是截取长度,而substring的两个参数一个是开始位置,一个是结束位置code

var b = "hello world, I like you, and you?";

console.log(b.substr()); //hello world, I like you, and you?
console.log(b.substr(13,10)); //I like you
console.log(b.substring(15,-9)); //hello world, I
console.log(b.substring(15,-1)); //hello world, I

因而可知,substr、substring和slice这三者来讲,各有所长(╯°O°)╯┻━┻
你能体会么~不能体会的就留个言咯对象

Array

filter

filter可以按必定条件筛选出知足条件的数据,好比在后台处理mongdb的id时,前台传过来的id必须得知足id.trim().length === 24ip

var b = [ 'hello', 'world,', 'I', 'like', 'you,', 'and', 'you?' ];

console.log(b.filter(function (item){
    return item.length < 5;
}));  //[ 'I', 'like', 'you,', 'and', 'you?' ]

concat

concat方法可以将多个数组组合成一个数组,这在异步处理数据最后进行组装的时候特别有用

var b = [ 'hello', 'world,', 'I', 'like'];
var a = [ 'you,', 'and', 'you?' ];
var c = [[1],[2]];

console.log(b.concat(a)); //[ 'hello', 'world,', 'I', 'like', 'you,', 'and', 'you?' ]
console.log(b.concat(a).concat(c)); //[ 'hello', 'world,', 'I', 'like', 'you,', 'and', 'you?', [ 1 ], [ 2 ] ]

不少时候,在最后处理数据的时候,这种数组中嵌套了数组的状况是比较烦的,不过很简单,underscore有方法可以轻松解决它,你知道吗?(๑•́ ₃ •̀๑)

forEach

forEach应该是用的最多的方法了吧,它的做用就是遍历数组中的每个值,而后你就能够对每个值进行操做了

var b = ['hello', 'world,', 'I', 'like', 'you,', 'and', 'you?'];

b.forEach(function (ele, index) {
    ele = ele + 1;
});
console.log(b); //[ 'hello', 'world,', 'I', 'like', 'you,', 'and', 'you?' ]

关于forEach,更准确的说对于Javascript中的对象,我一直以为本身的理解已经够解决遇到的通常问题了,可是这个forEach仍是给我带来了难题。好比上面这个,我对ele进行改变以后,b数组并无按照我想象中的改变,结果是一成不变。而在在个人经历中,forEach中对遍历的值进行改变,它最终的结果使可以被改变的。所以,在这个变与不变之间,我没有找到一条清晰的分界线…

map

说了forEach怎么能够没有map,同是遍历数组中的每个值,map可以将对这些值的操做进行返回为一个新的数组

var b = ['hello', 'world,', 'I', 'like', 'you,', 'and', 'you?'];

console.log(b.map(function (ele){
    ele = ele + 1;
    return ele;
})); //[ 'hello1', 'world,1', 'I1', 'like1', 'you,1', 'and1', 'you?1' ]

reduce

再说说这个reduce吧,reduce方法遍历数组的每个值,操做本身定,最后返回一个作了这些累积操做的值,经常使用来算和吧,实用的呢,它可以产生比filter更强力的功效,这个本身体会吧~

var summary = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(summary.reduce(function (pre, cur) {
    pre += cur;
    return pre;
}, 0)); //45

今天就先写这么多吧,细水长流~

你能够很厉害,也能够很坚强,baby~

如想了解更多,请移步个人博客

相关文章
相关标签/搜索