# new RendererCollection(collection)
Renderer集合对象,主要用于M3DModelCacheLayer的renderer属性需要多个Renderer样式的场景
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
collection |
Array.<Object> | render结构集合 这里对object属性结构有要求,需要使用指定两个属性: layerID:渲染图层ID,字符串类型 renderer:渲染器对象,BaseRenderer类型及其派生类 |
示例
const rendererCollection = new zondy.RendererCollection([
{
// 子图层ID
layerID: '1834734999',
// 渲染renderer
renderer: layerRenderer1
},
{
// 子图层ID
layerID: '1834734000',
// 渲染renderer
renderer: layerRenderer2
}
])
layer.renderer = rendererCollection
继承关系
成员变量
方法
# add(item, index)
添加一个元素到集合中
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
item |
* | 要添加的元素 |
index |
Number | 指的元素要添加的位置,如果不指定,默认添加到集合末尾 |
- Overrides:
添加完元素后的集合对象
示例
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 | 指的元素要添加的位置,如果不指定,默认添加到集合末尾 |
- Inherited From:
示例
const collection = new Collection()
collection.addMany([1, 2, 3])
const collection = new Collection([1, 2, 3])
collection.addMany([1, 2, 3], 2)
# all()
获取集合中的所有元素并返回一个数组
- Inherited From:
集合中元素构成的数组
示例
const collection = new Collection([1, 2, 3])
collection.all()
# at(index)
返回指定下标处的元素
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
index |
Number | 元素下标,默认从0开始,允许使用正整数和负整数,负整数从集合中的最后一个元素开始倒数 |
- Inherited From:
查询到的元素
示例
const collection = new Collection([1, 2, 3])
const item = collection.at(1)
# clone()
克隆并返回一个新集合对象
- Inherited From:
克隆后的集合对象
示例
const collection = new Collection([1, 2, 3])
const cloneObj = collection.clone()
# concat(collection)
将一个数组或者集合对象链接到当前集合对象末尾
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
collection |
Array | Collection | 要链接的数组或者集合对象 |
- Inherited From:
链接后的新集合对象
示例
const collection = new Collection([1, 2, 3])
collection.concat([{'a': 1}, {'b': 2}])
collection.concat(new Collection([4, 5, 6]))
# every(fn)
检测数组所有元素是否都符合指定条件(通过函数提供)
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 过滤条件 |
- Inherited From:
是否全部满足过滤条件
示例
const collection = new Collection([1, 2, 3])
const isNumber = collection.every(function(item) {
return typeof item === 'number'
})
# filter(fn)
通过一个过滤条件(函数提供)来过滤并返回元素
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 过滤条件,如果过滤条件不是函数,则返回undefined |
- Inherited From:
过滤后的新集合对象
示例
const collection = new Collection([1, 2, 3, 4, 5])
const newCollection = collection.filter(function(item) {
return item > 2
})
# find(fn)
返回第一个符合过滤条件(函数提供)的元素,如果没找到则返回undefined
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 过滤条件,如果过滤条件不是函数,也返回undefined |
- Inherited From:
第一个符合过滤条件的元素
示例
const collection = new Collection([1, 2, 3, 4, 5])
const item = collection.find(function(item) {
return item > 2
})
# findIndex(fn)
返回第一个符合过滤条件(函数提供)的元素的下标,如果没找到则返回-1
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 过滤条件 |
- Inherited From:
第一个符合过滤条件(函数提供)的元素的下标
示例
const collection = new Collection([1, 2, 3, 4, 5])
const index = collection.findIndex(function(item) {
return item > 1
})
# flatten(fn)
扁平化一个集合对象,通过传入的扁平化函数来指定要扁平化的对象数组,直到指定的对象数组为空或者元素数量为0,之后返回一个新集合对象
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 扁平化函数,通过该函数来指定要扁平化的对象数组,如果该函数不是一个函数对象,则返回undefined |
- Inherited From:
扁平化后的集合对象
示例
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方法 |
- Inherited From:
执行完函数后的集合对象
示例
const collection = new Collection([1, 2, 3])
collection.forEach(function(item) {
console.log("item:", item)
})
# getItemAt(index)
获取指定下标的元素,没找到则返回undefined
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
index |
Number | 元素下标 |
- Inherited From:
该下标下的元素
示例
const collection = new Collection([1, 2, 3])
const item = collection.getItemAt(1)
# includes(item)
判断集合中是否包含该元素
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
item |
* | 要检查的元素对象 |
- Inherited From:
集合中是否包含该元素
示例
const collection = new Collection([1, 2, 3])
const isInclude = collection.includes(2)
# indexOf(item, fromIndex)
返回元素在集合中的下标
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
item |
* | 要在集合中寻找的元素 |
fromIndex |
Number | 选择起始下标,默认为0,即从何处开始寻找 |
- Inherited From:
元素在集合中的下标
示例
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 | 分割符号 |
- Inherited From:
拼接完成的字符串
示例
const collection = new Collection([1, 2, 3])
const str = collection.join(',')
# lastIndexOf(item, fromIndex)
从集合的最末尾开始,寻找该元素,并返回元素下标
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
item |
* | 要寻找的元素 |
fromIndex |
Number | 从何处开始查找,默认为集合的长度-1 |
- Inherited From:
第一个找到的元素下标,未找到则返回-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 | 遍历函数 |
- Inherited From:
遍历后的新集合对象
示例
const collection = new Collection([1, 2, 3])
const newCollection = collection.map(function (item) {
return item + 1
})
# pop()
删除并返回集合的最后一个元素
- Inherited From:
集合的最后一个元素
示例
const collection = new Collection([1, 2, 3])
const item = collection.pop()
# push(item)
添加一个元素到集合中
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
item |
* | 要添加的元素 |
- Inherited From:
添加完元素后的集合对象
示例
const collection = new Collection([1, 2, 3])
collection.push(4)
# reduce(fn, baseValue)
接收一个函数做为累加器,将集合中的每一个元素(下标从小到大的顺序)进行累加,最后返回一个对象
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 累加器函数 |
baseValue |
* | 累加的基础对象 |
- Inherited From:
累加后的对象
示例
const collection = new Collection([1, 2, 3])
const sum = collection.reduce(function (reduceCarry, item) {
return reduceCarry += item
}, 0)
# reduceRight(fn, baseValue)
接收一个函数做为累加器,将集合中的每一个元素(下标从大到小的顺序)进行累加,最后返回一个对象
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 累加器函数 |
baseValue |
* | 累加的基础对象 |
- Inherited From:
累加后的对象
示例
const collection = new Collection([1, 2, 3])
const sum = collection.reduce(function (reduceCarry, item) {
console.log(item)
})
# reorder(item, index)
将指的元素调整到集合中指的下标位置
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
item |
* | 要排序的元素 |
index |
Number | 元素要调整到的位置 |
- Inherited From:
示例
const collection = new Collection([1, 2, 3])
collection.reorder(1, 2)
# reverse()
将集合对象中的元素反转,并返回新集合对象
- Inherited From:
新的集合对象
示例
const collection = new Collection([1, 2, 3])
const newCollection = collection.reverse()
# shift()
删除并返回集合的第一个元素(元素下标为0)
- Inherited From:
集合的第一个元素
示例
const collection = new Collection([1, 2, 3])
const item = collection.shift()
# slice(start, end)
截取集合中从start开始到end下标的元素,并返回一个新集合对象
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
start |
Number | 从0开始的元素小标 |
end |
Number | 截至的元素下标,不填则默认到集合最后一个元素 |
- Inherited From:
新集合对象
示例
const collection = new Collection([1, 2, 3, 4, 5])
const newCollection = collection.slice(1, 3)
# some(fn)
判断集合对象中的元素是否满足过滤条件,只要有一个元素满足过滤条件,则会返回true,如果都不满足过滤条件,则返回false
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 过滤条件,如果过滤条件不是函数,也返回false |
- Inherited From:
是否满足过滤条件
示例
const collection = new Collection([1, 2, 3, 4, 5])
const flag = collection.some(function (item) {
return item === 1
})
# sort(fn)
通过一个排序函数来对当前的集合进行排序,之后返回一个新集合对象,参考js的sort函数
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
fn |
function | 排序函数,不填则默认按从小到达排序 |
- Inherited From:
新的集合对象
示例
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 |
* | 要删除或添加的元素 |
- Inherited From:
新的集合对象
示例
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()
返回集合对象中元素组成的数组
- Inherited From:
集合对象中元素组成的数组
示例
const collection = new Collection([1, 2, 3])
collection.toArray()
# unique(key)
去除集合中的重复元素
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
key |
function | String | 指定一个去重函数或者要素的去重字段 |
- Inherited From:
去重后的集合对象
示例
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 |
* | 要添加的一个或多个元素 |
- Inherited From:
示例
const collection = new Collection([1, 2, 3])
collection.unshift(1, 2)
