yu-array
v1.0.1
Published
function for Array prototype
Downloads
3
Readme
yu-array
扩展Array.prototype函数
安装
npm install yu-array --save
引入
require('yu-array');
//或
import 'yu-array'
Array.prototype.nextEach
同步手动遍历
/* 执行next才会遍历下一个元素 */
['a','b','c','d','e','f'].nextEach((val,i,next)=>{
console.log(i); //0,1,2,3,4,5
next();
})
/* 间隔n个元素 */
['a','b','c','d','e','f'].nextEach((val,i,next)=>{
console.log(i); //0,2,4
next(1); //间隔1个元素遍历
})
/* 从指定索引开始遍历 */
['a','b','c','d','e','f'].nextEach((val,i,next)=>{
console.log(i); //1,3,5
next(1); //间隔1个元素遍历
},1) //从索引1开始遍历
/* 倒序 */
['a','b','c','d','e','f'].nextEach((val,i,next)=>{
console.log(i); //5,3,1
next(1); //间隔1个元素遍历
},1,true) //倒序
Array.prototype.asyncNextEach
异步手动遍历,返回promise
/* 执行next才会遍历下一个元素 */
['a','b','c','d','e','f'].asyncNextEach((val,i,next)=>{
console.log(i); //0,1,2,3,4,5
next();
}).then(()=>{
console.log('遍历完成')
})
/* 间隔n个元素 */
['a','b','c','d','e','f'].asyncNextEach((val,i,next)=>{
console.log(i); //0,2,4
next(1); //间隔1个元素遍历
})
/* 从指定索引开始遍历 */
['a','b','c','d','e','f'].asyncNextEach((val,i,next)=>{
console.log(i); //1,3,5
next(1); //间隔1个元素遍历
},1) //从索引1开始遍历
/* 倒序 */
['a','b','c','d','e','f'].asyncNextEach((val,i,next)=>{
console.log(i); //5,3,1
next(1); //间隔1个元素遍历
},1,true) //倒序
Array.prototype.timeoutEach
定时遍历,返回promise
/* 执行next才会遍历下一个元素 */
['a','b','c','d','e','f'].timeoutEach((val,i,next)=>{
console.log(i); //0,1,2,3,4,5
next();
}).then(()=>{
console.log('遍历完成')
})
/* 设定遍历的间隔时间 */
['a','b','c','d','e','f'].timeoutEach((val,i,next)=>{
console.log(i); //0,1,2,3,4,5
next();
},1000) //每隔1000毫秒遍历一个元素
/* 从指定索引开始遍历 */
['a','b','c','d','e','f'].timeoutEach((val,i,next)=>{
console.log(i); //1,2,3,4,5
next();
},1000,1) //从索引1开始遍历
/* 倒序 */
['a','b','c','d','e','f'].timeoutEach((val,i,next)=>{
console.log(i); //5,4,3,2,1
next();
},1000,1,true) //倒序
/* 间隔n个索引 */
['a','b','c','d','e','f'].timeoutEach((val,i,next)=>{
console.log(i); //0,2,4
next(1); //间隔1个元素遍历
})
/* 指定遍历下一个元素的间隔时间 */
['a','b','c','d','e','f'].timeoutEach((val,i,next)=>{
console.log(i); //0,2,4
next(1,500); //500毫秒后遍历下一个
})
Array.prototype.ectypeEach
副本遍历 (创建该数组的副本,用来遍历)
/* val是副本的元素,i是副本的索引值 */
let arr = ['a','b','c','d','e','f'];
arr.ectypeEach((val,i)=>{
console.log(i); //0,1,2,3,4,5
arr.splice(0); //删除原数组不影响遍历
})
/* x是原数组的索引值 */
let arr1 = ['a','b','c','d','e','f'];
arr1.ectypeEach((val,i,x)=>{
console.log(i); //0,1,2,3,4,5
i===2 && arr.splice(x,1); //删除原数组不影响遍历
console.log(x); //0,1,2,2,3,4 x是当前元素在原数组的索引值
})
/* 倒序 */
['a','b','c','d','e','f'].ectypeEach((val,i,x)=>{
console.log(i); //5,4,3,2,1,0
},true)
Array.prototype.bubbling
冒泡排序
/* 顺序 */
[1,3,5,4,2].bubbling() //[1,2,3,4,5]
/* 倒序 */
[1,3,5,4,2].bubbling(true) //[5,4,3,2,1]
Array.prototype.select
选择排序
/* 顺序 */
[1,3,5,4,2].select() //[1,2,3,4,5]
/* 倒序 */
[1,3,5,4,2].select(true) //[5,4,3,2,1]
Array.prototype.insertion
插入排序
/* 顺序 */
[1,3,5,4,2].insertion() //[1,2,3,4,5]
/* 倒序 */
[1,3,5,4,2].insertion(true) //[5,4,3,2,1]
Array.prototype.quick
快速排序
/* 顺序 */
[1,3,5,4,2].quick() //[1,2,3,4,5]
/* 倒序 */
[1,3,5,4,2].quick(true) //[5,4,3,2,1]