JavaScript Tutorial/Dojo toolkit/Array
Содержание
Array filter
<html>
<head>
<link rel="StyleSheet" type="text/css"
href="js/dojo/dijit/themes/tundra/tundra.css">
<script type="text/javascript">
var djConfig = {
baseScriptUri : "js/dojo/",
parseOnLoad : true
};
</script>
<script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
<script>
dojo.require("dojo.parser");
dojo.require("dojo.string");
dojo.addOnLoad(f);
function f(){
alert(dojo.filter([2,3,4], function(x) {return x % 2 == 0}));
}
</script>
</head>
<body class="tundra">
</body>
</html>
Array index of
<html>
<head>
<link rel="StyleSheet" type="text/css"
href="js/dojo/dijit/themes/tundra/tundra.css">
<script type="text/javascript">
var djConfig = {
baseScriptUri : "js/dojo/",
parseOnLoad : true
};
</script>
<script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
<script>
dojo.require("dojo.parser");
dojo.require("dojo.string");
dojo.addOnLoad(f);
function f(){
var foo = [1,2,3];
var bar = [4,5,6,5,6];
var baz = [1,2,3];
alert(dojo.indexOf([foo, bar], baz));
alert(dojo.indexOf(foo, 3));
alert(dojo.indexOf(bar, 6, 2));
alert(dojo.indexOf(bar, 6, 3));
alert(dojo.lastIndexOf(bar, 6));
}
</script>
</head>
<body class="tundra">
</body>
</html>
Array index offset
<html>
<head>
<link rel="StyleSheet" type="text/css"
href="js/dojo/dijit/themes/tundra/tundra.css">
<script type="text/javascript">
var djConfig = {
baseScriptUri : "js/dojo/",
parseOnLoad : true
};
</script>
<script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
<script>
dojo.require("dojo.parser");
dojo.require("dojo.string");
dojo.addOnLoad(f);
function f(){
var foo = [1,2,3];
var bar = [4,5,6,5,6];
var baz = [1,2,3];
alert(dojo.indexOf([foo, bar], baz));
alert(dojo.indexOf(foo, 3));
alert(dojo.indexOf(bar, 6, 2));
alert(dojo.indexOf(bar, 6, 3));
alert(dojo.lastIndexOf(bar, 6));
}
</script>
</head>
<body class="tundra">
</body>
</html>
Get element index in an array
<html>
<head>
<script type="text/javascript">
var djConfig = {
baseScriptUri : "js/dojo/"
};
</script>
<script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
<script>
var a = [ "A", "N", "K", "S", "A" ];
alert(dojo.indexOf(a, "A") + ", " + dojo.indexOf(a, "S") +
", " + dojo.indexOf(a, "N", 0, true));
</script>
</head>
<body></body>
</html>
Get item index
<html>
<head>
<script type="text/javascript">
var djConfig = {
baseScriptUri : "js/dojo/"
};
</script>
<script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
<script>
var a = [ "B", "J", "K", "A", "B" ];
alert(dojo.indexOf(a, "J") + ", " + dojo.indexOf(a, "B"));
</script>
</head>
<body></body>
</html>
Get last index of
<html>
<head>
<script type="text/javascript">
var djConfig = {
baseScriptUri : "js/dojo/",
parseOnLoad : true
};
</script>
<script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
<script>
function f(){
alert(dojo.lastIndexOf(["foo", "hoo", "zoo", "shoe", "zoo", "nuu"], "zoo"));
}
</script>
</head>
<body onload=f()>
</body>
</html>
Is array like
<html>
<head>
<script type="text/javascript">
var djConfig = {
baseScriptUri : "js/dojo/"
};
</script>
<script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
<script>
function doOnLoad() {
alert(dojo.isArrayLike(document.body.childNodes));
}
</script>
</head>
<body onLoad="doOnLoad();"><div id="myDiv">I am myDiv</div></body>
</html>
Is object an array
<html>
<head>
<script type="text/javascript">
var djConfig = {
baseScriptUri : "js/dojo/"
};
</script>
<script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
</head>
<body>
<script type="text/javascript">
var s = new String("I am a string");
var a = new Array();
var i = 5;
alert(dojo.isArray(s) + "\n" + dojo.isArray(a) + "\n" + dojo.isArray(i));
</script>
</body>
</html>