# new Collection(collection)
集合类用来存储相同类型的项的数组,提供了大量的实用方法来处理集合中的元素,包括filter()、find()和reduce()等。
集合中的元素可以是任何类型。例如,GraphicsLayer.graphics是存储在GraphicsLayer中的图形的集合。您可以使用Collection类中的方法来添加、删除、重新排序或操作GraphicsLayer中的图形。
集合的另一个示例是Map.layers,它是所有地图的集合。
参数:
名称 | 类型 | 描述 |
---|---|---|
collection |
Array.<*> | 构造参数,要传入的元素数组 |
支持如下方法:
[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、创建实例]
示例
const collection = new Collection([1, 2, 3])
成员变量
方法
# static isCollection(item)
判断传入的对象是否是一个集合对象
参数:
名称 | 类型 | 描述 |
---|---|---|
item |
* | 要判断的对象 |
是否是集合对象
示例
const collection = new Collection([1, 2, 3])
const isCollection = Collection.isCollection(collection)
# add(item, index)
添加一个元素到集合中
参数:
名称 | 类型 | 描述 |
---|---|---|
item |
* | 要添加的元素 |
index |
Number | 指的元素要添加的位置,如果不指定,默认添加到集合末尾 |
添加完元素后的集合对象
示例
const collection = new Collection()
collection.add({
key: 'value'
})
const collection = new Collection([1, 2, 3])
collection.add({
key: 'value'
}, 1)
# addMany(items, index)
添加多个元素到集合中,添加完成后会发送数据变更事件
参数:
名称 | 类型 | 描述 |
---|---|---|
items |
Array.<*> | 要添加的多个元素 |
index |
Number | 指的元素要添加的位置,如果不指定,默认添加到集合末尾 |
示例
const collection = new Collection()
collection.addMany([1, 2, 3])
const collection = new Collection([1, 2, 3])
collection.addMany([1, 2, 3], 2)
# all()
获取集合中的所有元素并返回一个数组
集合中元素构成的数组
示例
const collection = new Collection([1, 2, 3])
collection.all()
# at(index)
返回指定下标处的元素
参数:
名称 | 类型 | 描述 |
---|---|---|
index |
Number | 元素下标,默认从0开始,允许使用正整数和负整数,负整数从集合中的最后一个元素开始倒数 |
查询到的元素
示例
const collection = new Collection([1, 2, 3])
const item = collection.at(1)
# clone()
克隆并返回一个新集合对象
克隆后的集合对象
示例
const collection = new Collection([1, 2, 3])
const cloneObj = collection.clone()
# concat(collection)
将一个数组或者集合对象链接到当前集合对象末尾
参数:
名称 | 类型 | 描述 |
---|---|---|
collection |
Array | Collection | 要链接的数组或者集合对象 |
链接后的新集合对象
示例
const collection = new Collection([1, 2, 3])
collection.concat([{'a': 1}, {'b': 2}])
collection.concat(new Collection([4, 5, 6]))
# every(fn)
检测数组所有元素是否都符合指定条件(通过函数提供)
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 过滤条件 |
是否全部满足过滤条件
示例
const collection = new Collection([1, 2, 3])
const isNumber = collection.every(function(item) {
return typeof item === 'number'
})
# filter(fn)
通过一个过滤条件(函数提供)来过滤并返回元素
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 过滤条件,如果过滤条件不是函数,则返回undefined |
过滤后的新集合对象
示例
const collection = new Collection([1, 2, 3, 4, 5])
const newCollection = collection.filter(function(item) {
return item > 2
})
# find(fn)
返回第一个符合过滤条件(函数提供)的元素,如果没找到则返回undefined
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 过滤条件,如果过滤条件不是函数,也返回undefined |
第一个符合过滤条件的元素
示例
const collection = new Collection([1, 2, 3, 4, 5])
const item = collection.find(function(item) {
return item > 2
})
# findIndex(fn)
返回第一个符合过滤条件(函数提供)的元素的下标,如果没找到则返回-1
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 过滤条件 |
第一个符合过滤条件(函数提供)的元素的下标
示例
const collection = new Collection([1, 2, 3, 4, 5])
const index = collection.findIndex(function(item) {
return item > 1
})
# flatten(fn)
扁平化一个集合对象,通过传入的扁平化函数来指定要扁平化的对象数组,直到指定的对象数组为空或者元素数量为0,之后返回一个新集合对象
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 扁平化函数,通过该函数来指定要扁平化的对象数组,如果该函数不是一个函数对象,则返回undefined |
扁平化后的集合对象
示例
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)
对集合中的所有元素执行传入的函数
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 针对元素的执行函数,参考js数组的forEach方法 |
执行完函数后的集合对象
示例
const collection = new Collection([1, 2, 3])
collection.forEach(function(item) {
console.log("item:", item)
})
# getItemAt(index)
获取指定下标的元素,没找到则返回undefined
参数:
名称 | 类型 | 描述 |
---|---|---|
index |
Number | 元素下标 |
该下标下的元素
示例
const collection = new Collection([1, 2, 3])
const item = collection.getItemAt(1)
# includes(item)
判断集合中是否包含该元素
参数:
名称 | 类型 | 描述 |
---|---|---|
item |
* | 要检查的元素对象 |
集合中是否包含该元素
示例
const collection = new Collection([1, 2, 3])
const isInclude = collection.includes(2)
# indexOf(item, fromIndex)
返回元素在集合中的下标
参数:
名称 | 类型 | 描述 |
---|---|---|
item |
* | 要在集合中寻找的元素 |
fromIndex |
Number | 选择起始下标,默认为0,即从何处开始寻找 |
元素在集合中的下标
示例
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)
通过传入一个分隔符,将集合中的元素通过分隔符拼接成一个字符串并返回
参数:
名称 | 类型 | 描述 |
---|---|---|
separator |
String | 分割符号 |
拼接完成的字符串
示例
const collection = new Collection([1, 2, 3])
const str = collection.join(',')
# lastIndexOf(item, fromIndex)
从集合的最末尾开始,寻找该元素,并返回元素下标
参数:
名称 | 类型 | 描述 |
---|---|---|
item |
* | 要寻找的元素 |
fromIndex |
Number | 从何处开始查找,默认为集合的长度-1 |
第一个找到的元素下标,未找到则返回-1
示例
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)
通过一个遍历函数来遍历该集合下的所有元素,并返回一个新集合对象
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 遍历函数 |
遍历后的新集合对象
示例
const collection = new Collection([1, 2, 3])
const newCollection = collection.map(function (item) {
return item + 1
})
# pop()
删除并返回集合的最后一个元素
集合的最后一个元素
示例
const collection = new Collection([1, 2, 3])
const item = collection.pop()
# push(item)
添加一个元素到集合中
参数:
名称 | 类型 | 描述 |
---|---|---|
item |
* | 要添加的元素 |
添加完元素后的集合对象
示例
const collection = new Collection([1, 2, 3])
collection.push(4)
# reduce(fn, baseValue)
接收一个函数做为累加器,将集合中的每一个元素(下标从小到大的顺序)进行累加,最后返回一个对象
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 累加器函数 |
baseValue |
* | 累加的基础对象 |
累加后的对象
示例
const collection = new Collection([1, 2, 3])
const sum = collection.reduce(function (reduceCarry, item) {
return reduceCarry += item
}, 0)
# reduceRight(fn, baseValue)
接收一个函数做为累加器,将集合中的每一个元素(下标从大到小的顺序)进行累加,最后返回一个对象
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 累加器函数 |
baseValue |
* | 累加的基础对象 |
累加后的对象
示例
const collection = new Collection([1, 2, 3])
const sum = collection.reduce(function (reduceCarry, item) {
console.log(item)
})
# remove(item)
删除一个元素,仅会删除找到的第一个元素,并发送删除事件
参数:
名称 | 类型 | 描述 |
---|---|---|
item |
* | 要删除的元素 |
元素是否删除成功
示例
const collection = new Collection([1, 2, 3])
collection.remove(1)
# removeAll()
删除集合中的所有元素,删除完成后会发送数据变更事件
示例
const collection = new Collection([1, 2, 3])
collection.removeAll(1)
# removeAt(index)
删除指定下标的元素
参数:
名称 | 类型 | 描述 |
---|---|---|
index |
Number | 元素的下标,从0开始 |
删除后的元素
示例
const collection = new Collection([1, 2, 3])
collection.removeAt(1)
# removeMany(items)
删除集合中的多个元素,删除完成后会发送数据变更事件
参数:
名称 | 类型 | 描述 |
---|---|---|
items |
Array.<*> | 要添加的多个元素 |
被删除的元素
示例
const collection = new Collection([1, 2, 3])
collection.removeMany([1, 2])
# reorder(item, index)
将指的元素调整到集合中指的下标位置
参数:
名称 | 类型 | 描述 |
---|---|---|
item |
* | 要排序的元素 |
index |
Number | 元素要调整到的位置 |
示例
const collection = new Collection([1, 2, 3])
collection.reorder(1, 2)
# reverse()
将集合对象中的元素反转,并返回新集合对象
新的集合对象
示例
const collection = new Collection([1, 2, 3])
const newCollection = collection.reverse()
# shift()
删除并返回集合的第一个元素(元素下标为0)
集合的第一个元素
示例
const collection = new Collection([1, 2, 3])
const item = collection.shift()
# slice(start, end)
截取集合中从start开始到end下标的元素,并返回一个新集合对象
参数:
名称 | 类型 | 描述 |
---|---|---|
start |
Number | 从0开始的元素小标 |
end |
Number | 截至的元素下标,不填则默认到集合最后一个元素 |
新集合对象
示例
const collection = new Collection([1, 2, 3, 4, 5])
const newCollection = collection.slice(1, 3)
# some(fn)
判断集合对象中的元素是否满足过滤条件,只要有一个元素满足过滤条件,则会返回true,如果都不满足过滤条件,则返回false
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 过滤条件,如果过滤条件不是函数,也返回false |
是否满足过滤条件
示例
const collection = new Collection([1, 2, 3, 4, 5])
const flag = collection.some(function (item) {
return item === 1
})
# sort(fn)
通过一个排序函数来对当前的集合进行排序,之后返回一个新集合对象,参考js的sort函数
参数:
名称 | 类型 | 描述 |
---|---|---|
fn |
function | 排序函数,不填则默认按从小到达排序 |
新的集合对象
示例
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)
向集合中删除或添加一个元素,参考js的splice方法
参数:
名称 | 类型 | 描述 |
---|---|---|
start |
Number | 删除或添加的起始下标 |
deleteCount |
Number | 删除或添加的数量 |
items |
* | 要删除或添加的元素 |
新的集合对象
示例
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()
返回集合对象中元素组成的数组
集合对象中元素组成的数组
示例
const collection = new Collection([1, 2, 3])
collection.toArray()
# unique(key)
去除集合中的重复元素
参数:
名称 | 类型 | 描述 |
---|---|---|
key |
function | String | 指定一个去重函数或者要素的去重字段 |
去重后的集合对象
示例
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)
向集合起始位置添加一个或多个元素
参数:
名称 | 类型 | 描述 |
---|---|---|
items |
* | 要添加的一个或多个元素 |
示例
const collection = new Collection([1, 2, 3])
collection.unshift(1, 2)