Reduce,split,join,reverse

347 阅读1分钟

pre,若数组还未被操作,则pre为数组的第一项或者为初始化的init值,若已产生操作,则值为上一次操作所返回的值。

cur为要调用的数组元素,现在为1,若无初始的init值,则值为2

index 索引,arr是要操作的数组,init表示初始值

let array = [1,2,3,4]
array.reduce((x, y) => return x + y), [])

arr.reduce(function(prev,cur,index,arr){
	...some Code
}, init);

split 分割,将目标数组内与传参相同的值替代为"," 参数为空时,将字符串切割一个变量为单位的数组,参数为空格时,将字符串内的空格替代为","

const array = "hello,renhanhan,buluohe"
console.log(array.split("he"));

join 重组,将目标字符串重组为数组,把传参插入到字符串的间隔中,重组为新的字符串

const array = ['hello','buluohe']
console.log(array.join('---'));

reverse 翻转数组内的参数并返回原数组

const array = 'hellobuluoherenhanhan'
console.log(array.split("").reverse());