.. _quickstart/arrays:
Arrays and Dojo
===============
:Authors: Nikolai Onken, Marcus Reimann, Josh Trutwin
.. contents::
:depth: 2
Dojo comes with a bunch of useful methods to deal with arrays, a few more than you get from your browser by default.
============
dojo.indexOf
============
dojo.indexOf lets you easily determine the index of an element in an array. It locates the first index of the provided value in the passed array. If the value is not found, -1 is returned.
.. cv-compound::
.. cv :: javascript
.. cv :: html
The content of our test array is ["foo", "hoo", "zoo"].
================
dojo.lastIndexOf
================
dojo.lastIndexOf lets you easily determine the last index of an element in an array. It locates the last index of the provided value in the passed array. If the value is not found, -1 is returned.
.. cv-compound::
.. cv :: javascript
.. cv :: html
The content of our test array is ["foo", "hoo", "zoo", "shoe", "zoo", "nuu"].
============
dojo.forEach
============
This is a heavylifter you will use a lot when writing your apps using Dojo. dojo.forEach lets you iterate over arrays, node lists and provides you with ways to filter your results. Lets take a look at a very basic example.
Note the "i" variable which returns the current position of an iteration
.. cv-compound::
.. cv :: javascript
.. cv :: html
Now lets use dojo.forEach with a list of dom nodes we retrieve using dojo.query. Note that dojo.query returns the list of dom nodes as an array. This way you can easily iterate over each dom node using dojo.forEach
.. cv-compound::
.. cv :: javascript
.. cv :: html
===========
dojo.filter
===========
There are many cases when you have an array and want to filter it by a certain condition, say you have an array of people with a last name. You would like to filter those having a certain last name. Lets take a look at anexample
.. cv-compound::
.. cv :: javascript
.. cv :: html
Filtered items (only people with "Washington" as surname)
Unfiltered items (all people are represented in the list)
========
dojo.map
========
Another great function provided by Dojo is dojo.map. dojo.map lets you run a function on all elements of an array and returns a new array with the changed values. A very good example is the "Give all my employees a 10% salary rise":
.. cv-compound::
.. cv :: javascript
.. cv :: html
Peoples salaries after raise:
Peoples salaries before raise:
For complete documentation and more examples please check the :ref:`dojo.map documentation `
=========
dojo.some
=========
Imagine you are a manager of a famous bank. A client of you comes and visits your office asking for another million dollars as a credit.
Now your bank policies only allows you to give each client one credit over 1 million, not two, not three - though you may have several smaller credits. Even 3 credits a 500.000 - weird bank.. anyways. dojo.some is the perfect function to tell you whether an array has some of the asked values:
.. cv-compound::
.. cv :: javascript
.. cv :: html
The content of our test array is [200000, 500000, 350000, 1000000, 75, 3].
==========
dojo.every
==========
Lets get back to our bank manager. A client wants another credit, but you only allow a credit if every income transfer is at least 3000,-
An example:
.. cv-compound::
.. cv :: javascript
.. cv :: html
The content of our test array is [{'month': 'january', 'income': 2000}, {'month': 'february', 'income': 3200}, {'month': 'march', 'income': 2100}].