new Collection(collection)
支持如下方法:
[1、添加一个元素到集合中][2、添加一个元素到集合中的指定位置]
[3、添加多个元素到集合中]
[4、添加多个元素到集合中的指定位置]
[5、返回指定下标处的元素]
[6、克隆并返回一个新集合对象]
[7、将一个数组或者集合对象链接到当前集合对象末尾]
[8、检测数组所有元素是否都符合指定条件]
[9、通过一个过滤条件(函数提供)来过滤并返回元素]
[10、返回第一个符合过滤条件(函数提供)的元素]
[11、返回第一个符合过滤条件(函数提供)的元素的下标]
[12、扁平化一个集合对象]
[13、对集合中的所有元素执行传入的函数]
[14、获取指定下标的元素]
[15、判断集合中是否包含该元素]
[16、返回元素在集合中的下标]
[17、判断集合中是否包含该元素-选择起始下标]
[18、判断传入的对象是否是一个集合对象]
[19、通过传入一个分隔符,将集合中的元素通过分隔符拼接成一个字符串并返回]
[20、从集合的最末尾开始,寻找该元素,并返回元素下标]
[21、从集合的最末尾开始,寻找该元素,并返回元素下标 - 指定起始额位置]
[22、通过一个遍历函数来遍历该集合下的所有元素,并返回一个新集合对象]
[23、删除并返回集合的最后一个元素]
[25、接收一个函数做为累加器,将集合中的每一个元素(下标从小到大的顺序)进行累加,最后返回一个对象]
[26、接收一个函数做为累加器,将集合中的每一个元素(下标从大到小的顺序)进行累加,最后返回一个对象]
[27、删除一个元素,仅会删除找到的第一个元素,并发送删除事件]
[28、删除集合中的所有元素,删除完成后会发送数据变更事件]
[29、删除指定下标的元素]
[30、删除集合中的多个元素,删除完成后会发送数据变更事件]
[31、将指的元素调整到集合中指的下标位置]
[32、将集合对象中的元素反转,并返回新集合对象]
[33、删除并返回集合的第一个元素]
[34、截取集合中从start开始到end下标的元素,并返回一个新集合对象]
[35、判断集合对象中的元素是否满足过滤条件]
[36、通过一个排序函数来对当前的集合进行排序,之后返回一个新集合对象,参考js的sort函数]
[37、向集合中删除一个元素]
[38、向集合中添加一个元素]
[39、返回集合对象中元素组成的数组]
[40、向集合起始位置添加一个或多个元素]
[41、获取集合中的所有元素并返回一个数组]
[42、去除集合中的重复元素]
[43、创建实例]
集合类用来存储相同类型的项的数组,提供了大量的实用方法来处理集合中的元素,包括filter()、find()和reduce()等。
集合中的元素可以是任何类型。例如,GraphicsLayer.graphics是存储在GraphicsLayer中的图形的集合。您可以使用Collection类中的方法来添加、删除、重新排序或操作GraphicsLayer中的图形。
集合的另一个示例是Map.layers,它是所有地图的集合。
| Name | Type | Description |
|---|---|---|
collection |
Array.<*> |
构造参数,要传入的元素数组 |
Example
const collection = new Collection([1, 2, 3])
Members
-
itemsArray.<any>
-
数组集合中的元素
-
itemsArray.<any>
-
items
-
lengthNumber
-
数组集合的长度
Methods
-
Collection.isCollection(item){Boolean}
base/Collection.js, line 405 -
判断传入的对象是否是一个集合对象
Name Type Description item* 要判断的对象
Returns:
Type Description Boolean 是否是集合对象 Example
判断传入的对象是否是一个集合对象 const collection = new Collection([1, 2, 3]) const isCollection = Collection.isCollection(collection) -
add(item, index){Collection}
base/Collection.js, line 94 -
添加一个元素到集合中
Name Type Description item* 要添加的元素
indexNumber 指的元素要添加的位置,如果不指定,默认添加到集合末尾
Returns:
Type Description Collection 添加完元素后的集合对象 Examples
添加一个元素到集合中 const collection = new Collection() collection.add({ key: 'value' })添加一个元素到集合中的指定位置 const collection = new Collection([1, 2, 3]) collection.add({ key: 'value' }, 1) -
addMany(items, index)
base/Collection.js, line 115 -
添加多个元素到集合中,添加完成后会发送数据变更事件
Name Type Description itemsArray.<*> 要添加的多个元素
indexNumber 指的元素要添加的位置,如果不指定,默认添加到集合末尾
Examples
添加多个元素到集合中 const collection = new Collection() collection.addMany([1, 2, 3])添加多个元素到集合中的指定位置 const collection = new Collection([1, 2, 3]) collection.addMany([1, 2, 3], 2) -
all(){Array.<*>}
base/Collection.js, line 801 -
获取集合中的所有元素并返回一个数组
Returns:
Type Description Array.<*> 集合中元素构成的数组 Example
返回集合对象中元素组成的数组 const collection = new Collection([1, 2, 3]) collection.all() -
at(index){*}
base/Collection.js, line 140 -
返回指定下标处的元素
Name Type Description indexNumber 元素下标,默认从0开始,允许使用正整数和负整数,负整数从集合中的最后一个元素开始倒数
Returns:
Type Description * 查询到的元素 Example
返回指定下标处的元素 const collection = new Collection([1, 2, 3]) const item = collection.at(1) -
clone(){Collection}
base/Collection.js, line 156 -
克隆并返回一个新集合对象
Returns:
Type Description Collection 克隆后的集合对象 Example
克隆并返回一个新集合对象 const collection = new Collection([1, 2, 3]) const cloneObj = collection.clone() -
concat(collection){Collection}
base/Collection.js, line 173 -
将一个数组或者集合对象链接到当前集合对象末尾
Name Type Description collectionArray | Collection 要链接的数组或者集合对象
Returns:
Type Description Collection 链接后的新集合对象 Example
将一个数组或者集合对象链接到当前集合对象末尾 const collection = new Collection([1, 2, 3]) collection.concat([{'a': 1}, {'b': 2}]) collection.concat(new Collection([4, 5, 6])) -
createInstance(item){*}
base/Collection.js, line 868 -
创建实例,可由子类重载
Name Type Description item* 要创建的实例对象
Returns:
Type Description * 创建的实例对象 -
every(fn){Boolean}
base/Collection.js, line 206 -
检测数组所有元素是否都符合指定条件(通过函数提供)
Name Type Description fnfunction 过滤条件
Returns:
Type Description Boolean 是否全部满足过滤条件 Example
检测数组所有元素是否都符合指定条件 const collection = new Collection([1, 2, 3]) const isNumber = collection.every(function(item) { return typeof item === 'number' }) -
filter(fn){Collection}
base/Collection.js, line 233 -
通过一个过滤条件(函数提供)来过滤并返回元素
Name Type Description fnfunction 过滤条件,如果过滤条件不是函数,则返回undefined
Returns:
Type Description Collection 过滤后的新集合对象 Example
通过一个过滤条件(函数提供)来过滤并返回元素 const collection = new Collection([1, 2, 3, 4, 5]) const newCollection = collection.filter(function(item) { return item > 2 }) -
find(fn){*}
base/Collection.js, line 257 -
返回第一个符合过滤条件(函数提供)的元素,如果没找到则返回undefined
Name Type Description fnfunction 过滤条件,如果过滤条件不是函数,也返回undefined
Returns:
Type Description * 第一个符合过滤条件的元素 Example
返回第一个符合过滤条件(函数提供)的元素,如果没找到则返回undefined const collection = new Collection([1, 2, 3, 4, 5]) const item = collection.find(function(item) { return item > 2 }) -
findIndex(fn){Number}
base/Collection.js, line 282 -
返回第一个符合过滤条件(函数提供)的元素的下标,如果没找到则返回-1
Name Type Description fnfunction 过滤条件
Returns:
Type Description Number 第一个符合过滤条件(函数提供)的元素的下标 Example
返回第一个符合过滤条件(函数提供)的元素的下标,如果没找到则返回-1 const collection = new Collection([1, 2, 3, 4, 5]) const index = collection.findIndex(function(item) { return item > 1 }) -
flatten(fn){Collection}
base/Collection.js, line 300 -
扁平化一个集合对象,通过传入的扁平化函数来指定要扁平化的对象数组,直到指定的对象数组为空或者元素数量为0,之后返回一个新集合对象
Name Type Description fnfunction 扁平化函数,通过该函数来指定要扁平化的对象数组,如果该函数不是一个函数对象,则返回undefined
Returns:
Type Description Collection 扁平化后的集合对象 Example
扁平化一个集合对象 const collection = new Collection([{ layers: [1, 2, 3] },{ items: [4, 5, 6] }]) const flatten = collection.flatten(function(item) { return item.layers || item.items }) -
forEach(fn){Collection}
base/Collection.js, line 347 -
对集合中的所有元素执行传入的函数
Name Type Description fnfunction 针对元素的执行函数,参考js数组的forEach方法
Returns:
Type Description Collection 执行完函数后的集合对象 Example
对集合中的所有元素执行传入的函数 const collection = new Collection([1, 2, 3]) collection.forEach(function(item) { console.log("item:", item) }) -
getItemAt(index){*}
base/Collection.js, line 359 -
获取指定下标的元素,没找到则返回undefined
Name Type Description indexNumber 元素下标
Returns:
Type Description * 该下标下的元素 Example
获取指定下标的元素 const collection = new Collection([1, 2, 3]) const item = collection.getItemAt(1) -
includes(item){Boolean}
base/Collection.js, line 371 -
判断集合中是否包含该元素
Name Type Description item* 要检查的元素对象
Returns:
Type Description Boolean 集合中是否包含该元素 Example
判断集合中是否包含该元素 const collection = new Collection([1, 2, 3]) const isInclude = collection.includes(2) -
indexOf(item, fromIndex){Number}
base/Collection.js, line 393 -
返回元素在集合中的下标
Name Type Description item* 要在集合中寻找的元素
fromIndexNumber 选择起始下标,默认为0,即从何处开始寻找
Returns:
Type Description Number 元素在集合中的下标 Examples
判断集合中是否包含该元素 const item = { 'a': 1 } const collection = new Collection([item, 2, 3]) const index = collection.indexOf(item)判断集合中是否包含该元素-选择起始下标 const item = { 'a': 1 } const collection = new Collection([item, 2, 3, item]) const index = collection.indexOf(item, 1) -
join(separator){String}
base/Collection.js, line 417 -
通过传入一个分隔符,将集合中的元素通过分隔符拼接成一个字符串并返回
Name Type Description separatorString 分割符号
Returns:
Type Description String 拼接完成的字符串 Example
通过传入一个分隔符,将集合中的元素通过分隔符拼接成一个字符串并返回 const collection = new Collection([1, 2, 3]) const str = collection.join(',') -
lastIndexOf(item, fromIndex){Number}
base/Collection.js, line 438 -
从集合的最末尾开始,寻找该元素,并返回元素下标
Name Type Description item* 要寻找的元素
fromIndexNumber 从何处开始查找,默认为集合的长度-1
Returns:
Type Description Number 第一个找到的元素下标,未找到则返回-1 Examples
从集合的最末尾开始,寻找该元素,并返回元素下标 const collection = new Collection([1, 2, 3]) const index = collection.lastIndexOf(3)从集合的最末尾开始,寻找该元素,并返回元素下标 - 指定起始额位置 const collection = new Collection([1, 2, 3, 3, 3]) const index = collection.lastIndexOf(3, 3) -
map(fn){Collection}
base/Collection.js, line 460 -
通过一个遍历函数来遍历该集合下的所有元素,并返回一个新集合对象
Name Type Description fnfunction 遍历函数
Returns:
Type Description Collection 遍历后的新集合对象 Example
通过一个遍历函数来遍历该集合下的所有元素,并返回一个新集合对象 const collection = new Collection([1, 2, 3]) const newCollection = collection.map(function (item) { return item + 1 }) -
pop(){*}
base/Collection.js, line 481 -
删除并返回集合的最后一个元素
Returns:
Type Description * 集合的最后一个元素 Example
删除并返回集合的最后一个元素 const collection = new Collection([1, 2, 3]) const item = collection.pop() -
push(item){Collection}
base/Collection.js, line 493 -
添加一个元素到集合中
Name Type Description item* 要添加的元素
Returns:
Type Description Collection 添加完元素后的集合对象 Example
添加一个元素到集合中 const collection = new Collection([1, 2, 3]) collection.push(4) -
reduce(fn, baseValue){*}
base/Collection.js, line 508 -
接收一个函数做为累加器,将集合中的每一个元素(下标从小到大的顺序)进行累加,最后返回一个对象
Name Type Description fnfunction 累加器函数
baseValue* 累加的基础对象
Returns:
Type Description * 累加后的对象 Example
接收一个函数做为累加器,将集合中的每一个元素(下标从小到大的顺序)进行累加,最后返回一个对象 const collection = new Collection([1, 2, 3]) const sum = collection.reduce(function (reduceCarry, item) { return reduceCarry += item }, 0) -
reduceRight(fn, baseValue){*}
base/Collection.js, line 539 -
接收一个函数做为累加器,将集合中的每一个元素(下标从大到小的顺序)进行累加,最后返回一个对象
Name Type Description fnfunction 累加器函数
baseValue* 累加的基础对象
Returns:
Type Description * 累加后的对象 Example
接收一个函数做为累加器,将集合中的每一个元素(下标从大到小的顺序)进行累加,最后返回一个对象 const collection = new Collection([1, 2, 3]) const sum = collection.reduce(function (reduceCarry, item) { console.log(item) }) -
remove(item){Boolean}
base/Collection.js, line 556 -
删除一个元素,仅会删除找到的第一个元素,并发送删除事件
Name Type Description item* 要删除的元素
Returns:
Type Description Boolean 元素是否删除成功 Example
删除一个元素,仅会删除找到的第一个元素,并发送删除事件 const collection = new Collection([1, 2, 3]) collection.remove(1) -
removeAll()
base/Collection.js, line 574 -
删除集合中的所有元素,删除完成后会发送数据变更事件
Example
删除集合中的所有元素,删除完成后会发送数据变更事件 const collection = new Collection([1, 2, 3]) collection.removeAll(1) -
removeAt(index){*}
base/Collection.js, line 590 -
删除指定下标的元素
Name Type Description indexNumber 元素的下标,从0开始
Returns:
Type Description * 删除后的元素 Example
删除指定下标的元素 const collection = new Collection([1, 2, 3]) collection.removeAt(1) -
removeMany(items){Array.<*>}
base/Collection.js, line 607 -
删除集合中的多个元素,删除完成后会发送数据变更事件
Name Type Description itemsArray.<*> 要添加的多个元素
Returns:
Type Description Array.<*> 被删除的元素 Example
删除集合中的多个元素,删除完成后会发送数据变更事件 const collection = new Collection([1, 2, 3]) collection.removeMany([1, 2]) -
reorder(item, index)
base/Collection.js, line 641 -
将指的元素调整到集合中指的下标位置
Name Type Description item* 要排序的元素
indexNumber 元素要调整到的位置
Example
将指的元素调整到集合中指的下标位置 const collection = new Collection([1, 2, 3]) collection.reorder(1, 2) -
reverse(){Collection}
base/Collection.js, line 659 -
将集合对象中的元素反转,并返回新集合对象
Returns:
Type Description Collection 新的集合对象 Example
将集合对象中的元素反转,并返回新集合对象 const collection = new Collection([1, 2, 3]) const newCollection = collection.reverse() -
shift(){*}
base/Collection.js, line 671 -
删除并返回集合的第一个元素(元素下标为0)
Returns:
Type Description * 集合的第一个元素 Example
删除并返回集合的第一个元素 const collection = new Collection([1, 2, 3]) const item = collection.shift() -
slice(start, end){Collection}
base/Collection.js, line 684 -
截取集合中从start开始到end下标的元素,并返回一个新集合对象
Name Type Description startNumber 从0开始的元素小标
endNumber 截至的元素下标,不填则默认到集合最后一个元素
Returns:
Type Description Collection 新集合对象 Example
截取集合中从start开始到end下标的元素,并返回一个新集合对象 const collection = new Collection([1, 2, 3, 4, 5]) const newCollection = collection.slice(1, 3) -
some(fn){Boolean}
base/Collection.js, line 704 -
判断集合对象中的元素是否满足过滤条件,只要有一个元素满足过滤条件,则会返回true,如果都不满足过滤条件,则返回false
Name Type Description fnfunction 过滤条件,如果过滤条件不是函数,也返回false
Returns:
Type Description Boolean 是否满足过滤条件 Example
判断集合对象中的元素是否满足过滤条件 const collection = new Collection([1, 2, 3, 4, 5]) const flag = collection.some(function (item) { return item === 1 }) -
sort(fn){Collection}
base/Collection.js, line 734 -
通过一个排序函数来对当前的集合进行排序,之后返回一个新集合对象,参考js的sort函数
Name Type Description fnfunction 排序函数,不填则默认按从小到达排序
Returns:
Type Description Collection 新的集合对象 Example
通过一个排序函数来对当前的集合进行排序,之后返回一个新集合对象 const collection = new Collection([{ 'a': 2 },{ 'a': 1 },{ 'a': 3 }]) const newCollection = collection.sort(function (itemA, itemB) { return itemA.a - itemB.a }) -
splice(start, deleteCount, items){Collection}
base/Collection.js, line 763 -
向集合中删除或添加一个元素,参考js的splice方法
Name Type Description startNumber 删除或添加的起始下标
deleteCountNumber 删除或添加的数量
items* repeatable 要删除或添加的元素
Returns:
Type Description Collection 新的集合对象 Examples
向集合中删除一个元素 const collection = new Collection([1, 2, 3]) const newCollection = collection.splice(1, 1)向集合中添加一个元素 const collection = new Collection([1, 2, 3]) collection.splice(1, 0, 1) -
toArray(){Array}
base/Collection.js, line 775 -
返回集合对象中元素组成的数组
Returns:
Type Description Array 集合对象中元素组成的数组 Example
返回集合对象中元素组成的数组 const collection = new Collection([1, 2, 3]) collection.toArray() -
unique(key){Collection}
base/Collection.js, line 829 -
去除集合中的重复元素
Name Type Description keyfunction | String 指定一个去重函数或者要素的去重字段
Returns:
Type Description Collection 去重后的集合对象 Examples
No Arguments
const unique = new Collection([2, 1, 2, 3, 3, 4, 5, 1, 2]).unique(); console.log(unique); // [2, 1, 3, 4, 5]Property Name
const students = new Collection([ { name: 'Rick', grade: 'A'}, { name: 'Mick', grade: 'B'}, { name: 'Richard', grade: 'A'}, ]); // Students with unique grades. students.unique('grade'); // [{ name: 'Rick', grade: 'A'}, { name: 'Mick', grade: 'B'}]With Callback
const students = new Collection([ { name: 'Rick', grade: 'A'}, { name: 'Mick', grade: 'B'}, { name: 'Richard', grade: 'A'}, ]); // Students with unique grades. students.unique(s => s.grade); // [{ name: 'Rick', grade: 'A'}, { name: 'Mick', grade: 'B'}] -
unshift(items)
base/Collection.js, line 790 -
向集合起始位置添加一个或多个元素
Name Type Description items* repeatable 要添加的一个或多个元素
Example
返回集合对象中元素组成的数组 const collection = new Collection([1, 2, 3]) collection.unshift(1, 2)