Working with arrays in App Cloud

App Cloud apps are voracious consumers of JavaScript arrays, which carry all kinds of data from server to client: news articles, photo gallery metadata, a list of places near a user's location, and even tweets. You're probably familiar with the two basic techniques for iterating over arrays:

for (var i = 0; i < stuff.length; i++) {
    ...
}

and

for (var i in stuff) {
    ...
}

Now comes a third one:

stuff.forEach(function (val, idx, arr) {
    ...
});

Yeah, you read that right. And it doesn't use jQuery or any other sugar. Here's a complete example:

var fruits = ["apple", "banana", "cherry"];

fruits.forEach(function (val, idx, arr) {
    console.log(val, idx, arr);
});

Run the above code in your browser to see the output—then feel free to use this technique in your App Cloud template.

There are some other native JavaScript methods that might look new to you: every(), filter(), map() and some(). Read all about them in the MDN docs.

p.s. Get more tips and tricks (and share your own) by joining the Brightcove App Cloud discussion group on Google.