dojo 学习日记 之 数组操作

dojo 对Javascript1.6 的数组操作功能进行了拓展:

  • clearCache()
  • every(arr, callback, thisObject) Determines whether or not every item in arr satisfies the condition implemented by callback.
  • filter(arr, callback, thisObject) Returns a new Array with those items from arr that match the condition implemented by callback.
  • forEach(arr, callback, thisObject) for every item in arr, callback is invoked.
  • indexOf(arr, value, fromIndex, findLast) locates the first index of the provided value in the passed array.
  • lastIndexOf(arr, value, fromIndex) locates the last index of the provided value in the passed array.
  • map(arr, callback, thisObject, Ctr) applies callback to each element of arr and returns an Array with the results
  • some(arr, callback, thisObject) Determines whether or not any item in arr satisfies the condition implemented by callback.

 

1.every(arr,callback,thisObject);   校验是否数组arr内的所有元素都满足回调函数的条件,是返回true

Parameter

Type

Description

arr

Array | String

用来迭代的数组. 如果是string,则对string的没资格字符操作

callback

Function | String

回调函数被触发,三个参数: item, index, and array ,如果条件满足,返回为true

thisObject

Object

可选 可能作为回调函数的作用域

// returns false
array.every([1, 2, 3, 4], function(item){ return item>1; });

// returns true
array.every([1, 2, 3, 4], function(item){ return item>0; });

 

2.filter(arr, callback, thisObject) ; 返回一个满足过滤条件元素组成的数组

Parameter

Type

Description

arr

Array

the array to iterate over.

callback

Function | String

a function that is invoked with three arguments (item, index, array). The return of this function is expected to be a boolean which determines whether the passed-in item will be included in the returned array.

thisObject

Object

Optional

may be used to scope the call to callback

// Example
// returns [2, 3, 4]
array.filter([1, 2, 3, 4], function(item){ return item>1; });

 

3.forEach(arr, callback, thisObject) 对arr 的每一个元素,触发回调函数

// log out all members of the array:
array.forEach(
[ "thinger", "blah", "howdy", 10 ],
function(item){
console.log(item);
}
);

// log out the members and their indexes
array.forEach(
[ "thinger", "blah", "howdy", 10 ],
function(item, idx, arr){
console.log(item, "at index:", idx);
}
);

// use a scoped object member as the callback
 
var obj = {
prefix: "logged via obj.callback:",
callback: function(item){
console.log(this.prefix, item);
}
};

// specifying the scope function executes the callback in that scope
array.forEach(
[ "thinger", "blah", "howdy", 10 ],
obj.callback,
obj
);
// alternately, we can accomplish the same thing with lang.hitch()
array.forEach(
[ "thinger", "blah", "howdy", 10 ],
lang.hitch(obj, "callback")
);

 

4.indexOf(arr, value, fromIndex, findLast) 从fromIndex 起,返回arr数组中第一次出现value的索引值.

Parameter

Type

Description

arr

Array

 

value

Object

 

fromIndex

Integer

Optional

findLast

Boolean

Optional

Makes indexOf() work like lastIndexOf(). Used internally; not meant for external usage.

 

5.map(arr, callback, thisObject, Ctr) 根据回调函数映射数组值

Parameter

Type

Description

arr

Array | String

the array to iterate on. If a string, operates on individual characters.

callback

Function | String

a function is invoked with three arguments, (item, index, array), and returns a value

thisObject

Object

Optional

may be used to scope the call to callback

Ctr

undefined

 
// returns [2, 3, 4, 5]
array.map([1, 2, 3, 4], function(item){ return item+1 });

 

6.some(arr, callback, thisObject)  检验数组里是否有满足回调函数条件,有则返回true

Parameter

Type

Description

arr

Array | String

the array to iterate over. If a string, operates on individual characters.

callback

Function | String

a function is invoked with three arguments: item, index, and array and returns true if the condition is met.

thisObject

Object

Optional

may be used to scope the call to callback

// is true
array.some([1, 2, 3, 4], function(item){ return item>1; });

// is false
array.some([1, 2, 3, 4], function(item){ return item<1; });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值