On this page

# AgentArray

Subclass of Array with convenience methods used by NetLogo. Tipically 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:

Name | Type | Attributes | Description
--- | --- | --- | ---
`args` | * | <repeatable> | Zero or more items in Array

Source
  - AgentArray.js, line 7

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:

Name | Type | Description
--- | --- | ---
`array` | Array | Array to convert to AgentArray

Source
  - AgentArray.js, line 26

Returns:

array converted to AgentArray

Type:
  AgentArray

### 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:

Name | Type | Description
--- | --- | ---
`fcn` | function | fcn(element) return boolean

Source
  - AgentArray.js, line 121

Returns:

true if fcn returns true for all elements

Type:
  boolean

### 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:

Name | Type | Description
--- | --- | ---
`fcn` | function | fcn(agent, [index], [array])

Source
  - AgentArray.js, line 216

### atIndex() → {any}

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

Source
  - AgentArray.js, line 108

Returns:

Type:
  any

Example

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

### clone()

Create copy of this AgentArray

Source
  - AgentArray.js, line 309

Returns:

AgentArray

### first() → {any}

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

Source
  - AgentArray.js, line 85

Returns:

Type:
  any

Example

    aa.first()
     //=> { x: 0, y: 0 }

### 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:

Name | Type | Description
--- | --- | ---
`fcn` | function | fcn(agent, [index], [array])

Source
  - AgentArray.js, line 200

Returns:

Return this for chaining.

Type:
  this

### isEmpty() → {boolean}

Return true if there are no items in this Array

Source
  - AgentArray.js, line 74

Returns:

Type:
  boolean

Examples

    new AgentArray().isEmpty()
     //=> true

    aa.isEmpty()
     //=> false

### last() → {any}

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

Source
  - AgentArray.js, line 96

Returns:

Type:
  any

Example

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

### props(key, typeopt) → {Array}

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

Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`key` | String |  |  | Property name
`type` | Array | <optional> | AgentArray | Type of array (Array, Uint8Array, ...)

Source
  - AgentArray.js, line 139

Returns:

Array of given type

Type:
  Array

Examples

    aa.props('x')
     //=> [0, 0, 1]

    aa.props('y')
     //=> [0, 1, 0]

### 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:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`reporter` | function |  |  |
`ascending` | boolean | <optional> | true |

Source
  - AgentArray.js, line 328

Returns:

Type:
  AgentArray

### toArray() → {Array}

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

Source
  - AgentArray.js, line 50

Returns:

This AgentArray converted to Array

Type:
  Array

### 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:

Name | Type | Description
--- | --- | ---
`obj` | Object | Object of prop, array type pairs

Source
  - AgentArray.js, line 160

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

Source
  - AgentArray.js, line 186

Returns:

Type:
  AgentArray

### with(fcn) → {AgentArray}

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

Parameters:

Name | Type | Description
--- | --- | ---
`fcn` | function | fcn(agent, [index], [array])

Source
  - AgentArray.js, line 238

Returns:

Type:
  AgentArray

Type anything to view search result