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; });