ForEach 多功能循环
uni.$c.forEach(collection, callback,params = {})可循环“数组、对象、字符串、整数”,并返回处理后的新值。
回调中return false跳出循环,return null或undefined跳过当次循环,可以通过params配置。
基本使用
js
const arrRes = uni.$c.forEach([1,2,3],(value,key,index,info)=>{
if(value === 2) return null;
return value * 2
})
console.log(arrRes) // [2,6]
const objRes = uni.$c.forEach({a:1,b:2},(value,key,index,info)=>{
return value * 2
},{returnType:'object'})
console.log(objRes) // {a:2,b:4}1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
collection | Array,Object,String,Number | 待循环的对象 |
callback(value,key,index,info={first,last,length}) | Function | 回调函数value循环数字时值从1开始key循环对象时为键名,循环数组时为索引值index索引从0开始info循环信息:first是否为第一个元素,last是否为最后一个元素,length循环长度 |
params | Object | 配置参数returnType=array|object返回值类型,默认为arraycontinueValue跳出当次循环时返回的值,默认为null,undefinedbreakValue跳出循环的值,默认false |
