JavaScript Tutorial/Dojo toolkit/Dictionary
Содержание
Clear a dictionary
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
dy.clear();
alert(dy.count);
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Create a dictionary
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
var it = dy.getIterator();
while (!it.atEnd()) {
alert(it.get());
}
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Dictionary.containsKey
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
alert(dy.containsKey("R"));
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Dictionary.containsValue
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
alert(dy.containsValue("b"));
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Dictionary.count
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
dy.remove("B");
alert(dy.count);
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Dictionary.Entry
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
alert(dy.entry("B"));
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Dictionary key iterator
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
var keys = dy.getKeyList();
for (var i = 0; i < keys.length; i++) {
alert(keys[i]);
}
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Dictionary value iterator
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
var vals = dy.getValueList();
for (var i = 0; i < vals.length; i++) {
alert( vals[i]);
}
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Get Iterator from dictionary
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
var it = dy.getIterator();
while (!it.atEnd()) {
alert(it.get());
}
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>
Remove entry from Dictionary
<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>
dojo.require("dojox.collections.Dictionary");
function testIt() {
var dy = new dojox.collections.Dictionary();
dy.add("A", "a");
dy.add("B", "b");
dy.add("Q", "q");
dy.add("R", "r");
dy.remove("B");
alert(dy.count);
}
</script>
</head>
<body onLoad="testIt();">
</body>
</html>