AgentArray

Subclass of Array with convenience methods used by NetLogo. Typically the items in the array are Objects but can be any type.

Constructor

new AgentArray(…args)

Creates an instance of AgentArray. Simply pass-through to super() now, but may add initialization code later.

Parameters:
NameTypeAttributesDescription
args*<repeatable>

Zero or more items in Array

Example
let aa = new AgentArray({x:0,y:0}, {x:0,y:1}, {x:1,y:0})
 //=>  [{ x: 0, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 0 }]

Methods

(static) fromArray(array) → {AgentArray}

Convert an existing Array to an AgentArray "in place". Use array.slice() if a new array is wanted

Parameters:
NameTypeDescription
arrayArray

Array to convert to AgentArray

Returns:

array converted to AgentArray

Type: 
AgentArray

toArray() → {Array}

See World and MyClass's foo property. Convert this AgentArray to Array in-place

Returns:

This AgentArray converted to Array

Type: 
Array

isEmpty() → {boolean}

Return true if there are no items in this Array

Returns:
Type: 
boolean
Examples
new AgentArray().isEmpty()
 //=> true
aa.isEmpty()
 //=> false

first() → {any}

Return first item in this array. Returns undefined if empty.

Returns:
Type: 
any
Example
aa.first()
 //=> { x: 0, y: 0 }

last() → {any}

Return last item in this array. Returns undefined if empty.

Returns:
Type: 
any
Example
aa.last()
 //=>  { x: 1, y: 0 }

atIndex() → {any}

Return at index. Returns undefined if empty. Wrap the index to be within the array.

Returns:
Type: 
any
Example
aa.atIndex(aa.length)
 //=>  { x: 0, y: 0 }

all(fcn) → {boolean}

Return true if fcn(element) returns true for each element in this array. Same as Array.every, using NetLogo's name

Parameters:
NameTypeDescription
fcnfunction

fcn(element) return boolean

Returns:

true if fcn returns true for all elements

Type: 
boolean

props(key, typeopt) → {Array}

Return array of property values from this array's objects. Array type is specified, defaults to AgentArray

Parameters:
NameTypeAttributesDefaultDescription
keyString

Property name

typeArray<optional>
AgentArray

Type of array (Array, Uint8Array, ...)

Returns:

Array of given type

Type: 
Array
Examples
aa.props('x')
 //=> [0, 0, 1]
aa.props('y')
 //=> [0, 1, 0]

typedSample(obj) → {Object}

Creates an Object of Arrays, one Array per each property in obj. Obj is key, arrayType pairs: x: Float32Array This is advanced, used for web workers, very large data sets, and remote communication

Parameters:
NameTypeDescription
objObject

Object of prop, array type pairs

Returns:
Type: 
Object
Example
aa.typedSample({x: Uint8Array, y: Uint8Array})
 //=> {x: new Uint8Array([0, 0, 1]), y: new Uint8Array([0, 1, 0])}

uniq() → {AgentArray}

Return new AgentArray of the unique values of this array

Returns:
Type: 
AgentArray

forLoop(fcn) → {this}

Call fcn(agent, index, array) for each item in AgentArray. Index & array optional. Array assumed not mutable. Note: 5x+ faster than this.forEach(fcn)

Parameters:
NameTypeDescription
fcnfunction

fcn(agent, [index], [array])

Returns:

Return this for chaining.

Type: 
this

ask(fcn)

Call fcn(agent, [ i, AgentArray ]) for each agent in AgentArray. where i = agent's array index and AgentArray is this array Array can shrink. If it grows, will not visit beyond original length. If it either shrinks or grows, it will console.log a message "ask" is NetLogo term.

Parameters:
NameTypeDescription
fcnfunction

fcn(agent, [index], [array])

with(fcn) → {AgentArray}

Use: turtles.with(t => t.foo > 20).ask(t => t.bar = true)

Parameters:
NameTypeDescription
fcnfunction

fcn(agent, [index], [array])

Returns:
Type: 
AgentArray

clone()

Create copy of this AgentArray

Returns:

AgentArray

sortBy(reporter, ascendingopt) → {AgentArray}

Return this AgentArray sorted by the reporter in ascending/descending order. If reporter is a string, convert to a fcn returning that property.

Parameters:
NameTypeAttributesDefaultDescription
reporterfunction
ascendingboolean<optional>
true
Returns:
Type: 
AgentArray