日期:2014-05-16  浏览次数:20478 次

js,jquery,数组操作小结

?

?



----------------------定义数组----------------------
$(function(){
	var aArray = {};//定义一个数组
	aArray['name'] = "张三";
	aArray['sex'] = "男";
	aArray['mail'] = "123456@qq.com";//把值一个个添加到数组中。
	var arrayValue = aArray['name'];//取出其中一个值
 	alert(arrayValue);
	})
或者
$(function(){
	var aArray = {};//定义一个数组
	aArray[0] = "张三";
	aArray[1] = "男";
	aArray[2] = "123456@qq.com";//把值一个个添加到数组中。
	var arrayValue = aArray[0];//取出其中一个值
 	alert(arrayValue);
	}) 

------------------------------------------------------删除数组元素------------------------------------------

?

1.根据下标删除数组元素

?

test =?newArray();
//define removeByIndex method as part of the array
Array.prototype.removeByIndex =?function(index) {
????this.splice(index, 1);
}
test[0] =?'Apple';
test[1] =?'Ball';
test[2] =?'Cat';
test[3] =?'Dog';
alert("Array before removing elements: "+test);
test.removeByIndex(2);
alert("Array after removing elements: "+test);

2.根据值删除数组元素

?

test =?newArray();
Array.prototype.removeByValue =?function(val) {
????for(vari=0; i<this.length; i++) {
????????if(this[i] == val) {
????????????this.splice(i, 1);
????????????break;
????????}
????}
}
test[0] =?'Apple';
test[1] =?'Ball';
test[2] =?'Cat';
test[3] =?'Dog';
alert("Array before removing elements: "+test);
test.removeByValue('Cat');
alert("Array after removing elements: "+test);
------------------------------------------------------------

http://falchion.iteye.com/blog/186656

删除数组?
方法一?