Iterating an Array
Overview
This guides highlights the basic parts of javascript that can be used in davinci blog to add dynamic features or
powerful analytics. For programmers with experience programming python, the
python to javascript
guide may be more useful.
For Loop
One way to iterate through an array of items is to use a for loop.
let data = [1,2,3,4,5,6,7,8,9]
let total = 0;
for(let item of data){
total+=1;
}
Try it!
forEach
The forEach method on a list is similar to the forEach defined on a standard Javascript array. It iterates each element
of the list and executes a function that is passed to the forEach call.
let data = [1,2,3,4,5,6,7,8,9]
let total = 0;
data.forEach(p=>total +=1);
Try it!