- The Book of Dojo
- Quick Installation
- Hello World
- Debugging Tutorial
- Introduction
- Part 1: Life With Dojo
- Part 2: Dijit
- Part 3: JavaScript With Dojo and Dijit
- Part 4: Testing, Tuning and Debugging
- Part 5: DojoX
- The Dojo Book, 0.4
Array Functions
Submitted by fireflymantis on Wed, 06/06/2007 - 12:18.
Along with dojo.forEach, Dojo provides other array functions which mimic JavaScript 1.6 functionality. And that's good news for IE users, who don't have the luxury of JavaScript 1.6 yet.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Array Examples</title>
<script type="text/javascript"
src="http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js"></script>
<script type="text/javascript">
dojo.addOnLoad(function(){
// JavaScript is so important, it's listed twice!
var webLanguages = ["JavaScript", "PHP", "Rails", "JavaScript", "JSP", "ASP.NET"];
// indexOf and lastIndexOf are like the String functions
console.debug(dojo.indexOf(webLanguages, "JavaScript")); // = 0
console.debug(dojo.indexOf(webLanguages, "Javascript")); // = -1 - case sensitive!
console.debug(dojo.lastIndexOf(webLanguages, "JavaScript")); // = 3
// Apply function to each element and return a matching output array.
console.dir(
dojo.map(webLanguages,
function(elem) { return elem.length; }
)
); // Returns [10, 3, 5, 10, 3, 7]
// Filter applies a boolean function and returns an array of elements that match
console.dir(
dojo.filter(webLanguages,
function(elem) { return elem.substring(0,1) == 'J'; }
)
); // Returns ["JavaScript", "JavaScript", "JSP"]
// 'Every' and 'some' applies a boolean function to each element and returns true
// if function is true for all (every) or at least one (some)
console.debug(
dojo.every(webLanguages,
function(elem) { return elem > 'BASIC'; }
)
); // Returns false because ASP.NET is less than
// BASIC (technical arguments aside...)
console.debug(
dojo.some(webLanguages,
function(elem) { return elem.length < 4; }
)
); // Returns true because length of JSP and PHP are less than 4
});
</script>
</head>
</html>
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Array Examples</title>
<script type="text/javascript"
src="http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js"></script>
<script type="text/javascript">
dojo.addOnLoad(function(){
// JavaScript is so important, it's listed twice!
var webLanguages = ["JavaScript", "PHP", "Rails", "JavaScript", "JSP", "ASP.NET"];
// indexOf and lastIndexOf are like the String functions
console.debug(dojo.indexOf(webLanguages, "JavaScript")); // = 0
console.debug(dojo.indexOf(webLanguages, "Javascript")); // = -1 - case sensitive!
console.debug(dojo.lastIndexOf(webLanguages, "JavaScript")); // = 3
// Apply function to each element and return a matching output array.
console.dir(
dojo.map(webLanguages,
function(elem) { return elem.length; }
)
); // Returns [10, 3, 5, 10, 3, 7]
// Filter applies a boolean function and returns an array of elements that match
console.dir(
dojo.filter(webLanguages,
function(elem) { return elem.substring(0,1) == 'J'; }
)
); // Returns ["JavaScript", "JavaScript", "JSP"]
// 'Every' and 'some' applies a boolean function to each element and returns true
// if function is true for all (every) or at least one (some)
console.debug(
dojo.every(webLanguages,
function(elem) { return elem > 'BASIC'; }
)
); // Returns false because ASP.NET is less than
// BASIC (technical arguments aside...)
console.debug(
dojo.some(webLanguages,
function(elem) { return elem.length < 4; }
)
); // Returns true because length of JSP and PHP are less than 4
});
</script>
</head>
</html>
- Printer-friendly version
- Login or register to post comments
- Unsubscribe post
Error in filter comment
Looks like the filter item incorrectly lists "PHP" as a result:
// Returns ["JavaScript", "JavaScript", "PHP"]
probably should be
// Returns ["JavaScript", "JavaScript", "JSP"]