今天在看到 JavaScript 中 Array 这一部分的时候,发现字符串也可以作为数组的下标,以前还真不知道,孤陋寡闻。。。。
既然提供了这个特性,那么就有它的存在价值,我们来看看有什么用,欢迎大家补充。
首先,Array 是从 Object 继承下来,所以 Array 具有 Object 有的任何能力。看看Object作为集合的情况:
新建: var object = new Object(); 或 var object =
{};
增加: object[strIndex ] = value; (strIndex 为string)
删除: delete object[strIndex ];
遍历: for ( var strObjIndex in object ) object[strObjIndex ];
增加: object[strIndex ] = value; (strIndex 为string)
删除: delete object[strIndex ];
遍历: for ( var strObjIndex in object ) object[strObjIndex ];
code 1:
var object = {}; // var object = new Object(); 二者等价
object[ " First " ] = " Firstmm " ;
object[ " Second " ] = " Secondmm " ;
object[ " Third " ] = " Thirdmm " ;
delete object[ " Second " ];
for ( var strObjIndex in object )
alert( " strObjIndex = " + strObjIndex + "\nobject[" +strObjIndex + "] = "+ object[strObjIndex]);
object[ " First " ] = " Firstmm " ;
object[ " Second " ] = " Secondmm " ;
object[ " Third " ] = " Thirdmm " ;
delete object[ " Second " ];
for ( var strObjIndex in object )
alert( " strObjIndex = " + strObjIndex + "\nobject[" +strObjIndex + "] = "+ object[strObjIndex]);
除了我们熟悉的方法外,Array也是可以用字符串做下标,像code 1 中的 Object 一样。
code 2:
var array = new Array(); // var array = []; 二者等价
array[ " First " ] = " Firstgg " ;
array[ " Second " ] = " Secondgg " ;
array[ " Third " ] = " Thirdgg " ;
delete array[ " First " ];
for ( var strArrayIndex in array )
alert( " strArrayIndex = " + strArrayIndex + "\narray[" +strArrayIndex + "] = " + array[strArrayIndex]);
array[ " First " ] = " Firstgg " ;
array[ " Second " ] = " Secondgg " ;
array[ " Third " ] = " Thirdgg " ;
delete array[ " First " ];
for ( var strArrayIndex in array )
alert( " strArrayIndex = " + strArrayIndex + "\narray[" +strArrayIndex + "] = " + array[strArrayIndex]);
在某些情况下,用字符串做下标可以提高我们的访问效率,
例如我们要在Array中检索出一个指定的值,通常做法,我们需要遍历整个数组,如下:
code 3:
var arrayTest = new Array(); // var arrayTest = []; 二者等价
// 初始化arrayTest
arrayTest.push( " Blog " ); // arrayTest[0] = "Blog"; 二者等价
arrayTest.push( " Java " );
arrayTest.push( " MaJianan " );
var output = "" ; // 用于输出
// 我们需要检索出值为 Majianan 的元素,通常做法如下:
for ( var i = 0 ; i < arrayTest.length;i ++ ){
if (arrayTest[i] == " MaJianan " ){
output = " 用整数做下标的Array:哈哈! ^+^ \n " ;
output = output + " 第 " + i + " 次才找到\n " ;
output = output + " arrayTest[ " + i + " ] = " + arrayTest[i];
alert(output);
} else {
output = " 用整数做下标的Array:555555555![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
\n "
;
output = output + " 第 " + i + " 次没有找到\n " ;
output = output + " 找到的是arrayTest[ " + i + " ] = " + arrayTest[i]
alert(output);
}
}
// 初始化arrayTest
arrayTest.push( " Blog " ); // arrayTest[0] = "Blog"; 二者等价
arrayTest.push( " Java " );
arrayTest.push( " MaJianan " );
var output = "" ; // 用于输出
// 我们需要检索出值为 Majianan 的元素,通常做法如下:
for ( var i = 0 ; i < arrayTest.length;i ++ ){
if (arrayTest[i] == " MaJianan " ){
output = " 用整数做下标的Array:哈哈! ^+^ \n " ;
output = output + " 第 " + i + " 次才找到\n " ;
output = output + " arrayTest[ " + i + " ] = " + arrayTest[i];
alert(output);
} else {
output = " 用整数做下标的Array:555555555
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
output = output + " 第 " + i + " 次没有找到\n " ;
output = output + " 找到的是arrayTest[ " + i + " ] = " + arrayTest[i]
alert(output);
}
}
code 4:
// 使用字符串做数组下标
var arrayTest1 = [];
arrayTest1[ " blog " ] = " Blog " ;
arrayTest1[ " java " ] = " Java " ;
arrayTest1[ " majianan " ] = " MaJianan " ;
var key = " majianan " ;
if ( typeof (arrayTest1[key]) == " undefined " ){
output = " 用字符串做下标的Array:555555555![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
\n "
;
output = output + " 没找到下标为 " + key + " 的东东。\n " ;
output = output + " 明明没有这个东西,还让我找,耍我呢吧!!! " ;
alert(output);
// do sth
} else {
output = " 用字符串做下标的Array:哈哈! ^+^ 一次搞定\n " ;
output = output + " 找到arrayTest1[ " + key + " ] = " + arrayTest1[key];
alert(output);
// do sth
}
var key1 = " hello " ;
if ( typeof (array[key1]) == " undefined " ){
output = " 用字符串做下标的Array:555555555![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
\n "
;
output = output + " 没找到下标为 " + key1 + " 的东东。\n " ;
output = output + " 明明没有这个东西,还让我找,耍我呢吧!!! " ;
alert(output);
// do sth
} else {
output = " 用字符串做下标的Array:哈哈! ^+^ 一次搞定\n " ;
output = output + " 找到arrayTest1[ " + key1 + " ] = " + arrayTest1[key1];
alert(output);
// do sth
}
var arrayTest1 = [];
arrayTest1[ " blog " ] = " Blog " ;
arrayTest1[ " java " ] = " Java " ;
arrayTest1[ " majianan " ] = " MaJianan " ;
var key = " majianan " ;
if ( typeof (arrayTest1[key]) == " undefined " ){
output = " 用字符串做下标的Array:555555555
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
output = output + " 没找到下标为 " + key + " 的东东。\n " ;
output = output + " 明明没有这个东西,还让我找,耍我呢吧!!! " ;
alert(output);
// do sth
} else {
output = " 用字符串做下标的Array:哈哈! ^+^ 一次搞定\n " ;
output = output + " 找到arrayTest1[ " + key + " ] = " + arrayTest1[key];
alert(output);
// do sth
}
var key1 = " hello " ;
if ( typeof (array[key1]) == " undefined " ){
output = " 用字符串做下标的Array:555555555
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
output = output + " 没找到下标为 " + key1 + " 的东东。\n " ;
output = output + " 明明没有这个东西,还让我找,耍我呢吧!!! " ;
alert(output);
// do sth
} else {
output = " 用字符串做下标的Array:哈哈! ^+^ 一次搞定\n " ;
output = output + " 找到arrayTest1[ " + key1 + " ] = " + arrayTest1[key1];
alert(output);
// do sth
}
Array的这个特性(用字符串做下标)可以用来高效的检索Unique的字符串集合。
遍历用整数做下标的Array的时间复杂度是O(n),而遍历用字符串做下标的Array的时间复杂度是O(1)。
转:http://www.blogjava.net/majianan/archive/2006/11/09/80184.aspx