前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >dojo 学习日记 之 数组操作

dojo 学习日记 之 数组操作

作者头像
亦山
发布2019-05-25 09:52:23
3750
发布2019-05-25 09:52:23
举报

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1432230

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

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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// Example
// returns [2, 3, 4]
array.filter([1, 2, 3, 4], function(item){ return item>1; });

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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// 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; });
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年02月09日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档