AnyChart
API Reference
Still have questions?
Contact support
Top

class anychart.data.Iterator Improve this Doc

anychart.data.Iterator class is used to work with data in a View.
The iterator allows getting data from a anychart.data.View by crawling through rows. Iterator can be obtained using anychart.data.View#getIterator method and has methods to control current index and get values from data/metadata fields in a current row.

Methods Overview

Data
advance()Advances the iterator to the next item.
get()Gets the value from the current row by the field name.
getIndex()Returns the index of the item to which iterator points to.
getRowsCount()Returns the number of rows in the view.
meta()Metadata settings.
reset()Resets the data iterator to its zero state (before the first item of the view).
select()Sets a passed index as the current index and returns it in case of success.

Methods Description

advance

Advances the iterator to the next item.

Returns:

boolean - True Returns True if moved to the next item, otherwise returns False.
Example.
// Go to the beginning and then iterate through the whole set:
iterator.reset();
while(iterator.advance()){
   // do something
}

get

Gets the value from the current row by the field name.

Params:

NameTypeDescription
fieldNamestringThe name of the field to be fetched from the current row.

Returns:

* - The field value or undefined, if not found.
Example.
// Sample data set:
[
   {name: 'Kate', age: 27, contact: 6597439},
   {name: 'Billy', age: 31, contact: 6597789},
   {name: 'Margaret', age: 24, contact: 6597522},
   {name: 'John', age: 39, contact: 6597001},
]
// Returns 'Margaret' to the currentName:
if (iterator.select(2)){
  var name = iterator.get('name');
}

getIndex

Returns the index of the item to which iterator points to.

Returns:

number - The index of an iterator position.
Example.
// Data in some working state.
// Current index is marked with '=()=':
[17.22, 31.23, 12.2141, 123.3231, =(0.123)=, 34141.1, 2332.0, 12148.91]
// iterator.getIndex() will return 4.

getRowsCount

Returns the number of rows in the view.

Returns:

number - The number of rows in the set.
Example.
// Data
[
   {name: 'Kate', age: 27, contact: 6597439},
   {name: 'Billy', age: 31, contact: 6597789},
   {name: 'Margaret', age: 24, contact: 6597522},
   {name: 'John', age: 39, contact: 6597001},
]
// iterator.getRowsCount() will return 4.

meta

Gets the metadata value by the field name.

Params:

NameTypeDescription
namestringThe name of a metadata field.

Returns:

* - Returns metadata field value.
Example.
// Data                                              Metadata
[                                                     [
   {name: 'Kate', age: 27, contact: 6597439},            {},
   {name: 'Billy', age: 31, contact: 6597789},           {},
   {name: 'Margaret', age: 24, contact: 6597522},        {},
   {name: 'John', age: 39, contact: 6597001},            {}
]                                                     ]
iterator.select(2);
iterator.meta('name'); // will return undefined.
Sets metadata value by the field name.

Params:

NameTypeDescription
namestringThe name of a metadata field.
value*The value to be set.

Returns:

anychart.data.Iterator - Self instance for method chaining.
Example.
iterator.select(2);
iterator.meta('name', 'SampleText');
// After this call sample data set looks like this:
// Data                                              Metadata
[                                                     [
   {name: 'Kate', age: 27, contact: 6597439},            {},
   {name: 'Billy', age: 31, contact: 6597789},           {},
   {name: 'Margaret', age: 24, contact: 6597522},        {'name': 'SampleText'},
   {name: 'John', age: 39, contact: 6597001},            {}
]                                                     ]
// Note! Setter sets any passed values "as is", e.g.:
iterator.meta('smth', null);                             {'smth': null}
iterator.meta('smth', undefined);                        {'smth': undefined}
iterator.meta('smth', function(){ do_smth; });           {'smth': function(){ do_smth; }}

reset

Resets the data iterator to its zero state (before the first item of the view).

Returns:

anychart.data.Iterator - Self instance for method chaining.
Example.
// Data in some working state.
// Current index is marked with '=()=':
[17.22, 31.23, 12.2141, 123.3231, =(0.123)=, 34141.1, 2332.0, 12148.91]
// After Iterator.reset() is called current index is moved to -1 position.
=()= [17.22, 31.23, 12.2141, 123.3231, 0.123, 34141.1, 2332.0, 12148.91]

select

Sets a passed index as the current index and returns it in case of success.

Params:

NameTypeDescription
indexnumberThe index to select.

Returns:

boolean - True Returns true only when index is within a possible range, otherwise returns False.
Example.
// Data in some working state.
// Current index is marked with '=()=':
[ =(17.22)=, 31.23, 12.2141, 123.3231, 0.123, 34141.1, 2332.0, 12148.91]
// After iterator.select(4) is called current index is moved:
[17.22, 31.23, 12.2141, 123.3231, =(0.123)=, 34141.1, 2332.0, 12148.91]