55   JS

1,map,遍历数组的值进行处理,并返回新的数组值

var arr=[1,2,3]
console.log(arr);

var newArr = arr.map(function(v,i,a){
	console.log(v);
	console.log(i);
	console.log(a);
	return v+1;
})
console.log(arr);
console.log(newArr);

2,some,只要数组中的一个数值满足条件即可返回true

var arr = [1, 2, 3, 4, 5];
var rtn = arr.some(function (v, i, a) {
  	console.log(v);
	console.log(i);
	console.log(a);
    return v >= 3;
});
console.log(rtn);

3,every,数组中的所有值都满足条件才返回true,否则false

var arr = [1, 2, 3, 4, 5];
var rtn = arr.every(function (v, i, a) {
  	console.log(v);
	console.log(i);
	console.log(a);
    return v >= 3;
});
console.log(rtn);

4,reduce,遍历数组的值进行累加,第一个参数默认数组第一个值,第二参数默认数组第二值,之后第一个参数为累加值,第二参数为数组下一个值

var arr = [1, 2, 3, 4, 5];
var rtn = arr.reduce(function (one,two) {
  	console.log(one);
	console.log(two);
    return one+two;
});
console.log(rtn);




Leave a Reply

Your email address will not be published. Required fields are marked *