怎么销毁数组的一项
Array对象的已有方法中并没有remove。只有pop,push.........
我想指定key删除。
var nd = new Array();
nd[1] = 9;
nd[5]=12;
nd[3]=5;
....
能实现按key来删除吗?
nd.remove(5); ?
nd.delete(5); ?
nd.die(5); ?????
问题点数:30、回复次数:9Top
1 楼mingxuan3000(铭轩)回复于 2006-03-04 19:24:42 得分 2
学习Top
2 楼epbon(没有)回复于 2006-03-04 19:33:03 得分 5
js中通过设置数组的length属性来截断数组,这样可以缩短数组长度.如果使用delete运算符来删除数组中元素,虽然那个元素变成未定义的,但是数组的length属性不改变.
以前看到下面一种方法:
/*
* 方法:Array.remove(dx)
* 功能:删除数组元素.
* 参数:dx删除元素的下标.
* 返回:在原数组上修改数组
*/
//经常用的是通过遍历,重构数组.
Array.prototype.remove=function(dx)
{
if(isNaN(dx)||dx>this.length){return false;}
for(var i=0,n=0;i<this.length;i++)
{
if(this[i]!=this[dx])
{
this[n++]=this[i]
}
}
this.length-=1
}
a = ['1','2','3','4','5'];
alert("elements: "+a+"\nLength: "+a.length);
a.remove(0); //删除下标为0的元素
alert("elements: "+a+"\nLength: "+a.length);
Top
3 楼epbon(没有)回复于 2006-03-04 19:58:44 得分 3
说明:
上述代码对含重复元素的数组不适用。楼主有空可以自行完善一下:)Top
4 楼zhaoxiaoyang(梅雪香@深圳)回复于 2006-03-04 21:06:39 得分 8
OBJECT: Array
--------------------------------------------------------------------------------
new Array(arrayLength)
new Array(element0, element1, ..., elementN)
An array is an ordered set of values grouped together under a single variable name created by using an Array object constructor. You can create an Array literal by specifying the name of the array and the values of all its elements. The following example creates an array of three elements:
Code:
cars = new Array("Mercedes", "Ford", "Chrysler")
The elements of an array are indexed using their ordinal number, starting with 0. You could, therefore, refer to the second element in the above array ("Ford") as 'cars[1]'. You can specify the number of elements in a new array by using a single numeric parameter with the Array constructor.
For example, the following code creates an array of 7 elements:
Code:
fruit = new Array(7)
If you create an array with a single numeric parameter, that number is stored in the length property, and the array doesn't actually have any elements until some are specifically assigned to it. If, however, the parameter is not a number, an array of 1 element is created and that value assigned to it. You can easily increase the size of an array by assigning a value to an element higher than its current length.
NOTE:
If you specify 'language="Javascript1.2"' in the <SCRIPT> tag and use a single numeric parameter with the Array constructor, it will be seen as the value of a single element of the array rather than the number of elements you want that array to contain.
PROPERTIES
constructor Property
The constructor property contains the function that created an object's prototype.
Syntax: object.constructor
index Property
The read-only index property for an array created by a regular expression match and containing the zero-based index of that match.
Syntax: object.index
input Property
The read-only input property for an array created by a regular expression match and containing the original string against which the match was made.
Syntax: object.input
length Property
The length property holds an unsigned 32 bit integer representing the length of the array. It can be altered independently of the number of elements in the array.
Syntax: object.length
prototype Property
The prototype property allows the addition of properties to an array.
Syntax: object.prototype
METHODS
concat Method
The concat method joins two or more Array objects producing one new one. The original Array objects are unaffected by this but, if one copy of a string or number is altered, it is not reflected in the other, whereas a change to an object reference can be seen in both copies.
Syntax: Array.concat(arrayName2, arrayName3, ..., arrayNameN)
join Method
The join method is used to join all the elements of an array into a single string separated by a specified string separator (if none is specified, the default is a comma).
Syntax: Array.join(separator)
pop Method
The pop method is used to remove and return the last element of an array. This affects the length of the array.
Syntax: Array.pop()
push Method
The push method is used to add one or more elements to an array, returning the new length of it. This affects the length of the array.
Syntax: Array.push(element1, ..., elementN)
reverse Method
The reverse method, as the name implies, reverses the order of the elements in an array making the first last and the last first. Syntax: Array.reverse()
shift Method
The shift method removes and returns the first element of an array. This affects the length of the array.
Syntax: Array.shift()
slice Method
The slice method creates a new array from a selected section of an array.
Syntax: Array.slice(begin[,end])
splice Method
The splice method is used to add and/or remove elements of an array.
Syntax; Array.splice(index, howMany, [element1][, ..., elementN])
sort Method
The sort method sorts the elements of an array.
Syntax: Array.sort(compareFunction)
toSource Method
The toSource method is inherited from the Object object and returns the source code of the array. For details see the Object.toSource method.
Syntax: Array.toSource()
toString Method
The toString method is inherited from the Object object and returns a string representing the specified array and its elements. For more details see the Object.toString method.
Syntax: Array.toString()
unshift Method
The unshift method adds one or more elements to the beginning of an array and returns the new length.
Syntax: Array.unshift(element1,..., elementN)
valueOf Method
The valueOf method is inherited from the Object object and returns a primitive value for a specified array. For details see the Object.valueOf method.
Syntax: Array.valueOf()
其中pop和splice方法可用于删除数组中的一项.Top
5 楼ice_berg16(寻梦的稻草人)回复于 2006-03-04 22:27:04 得分 10
<script language="javascript">
var nd = new Array();
nd[1] = 9;
nd[5]=12;
nd[3]=5;
alert(nd);
nd.splice(3,1);
alert(nd);
</script>Top
6 楼hax(海曦)回复于 2006-03-05 00:01:07 得分 2
delete a[x]
a.splice(x, 1)
Top
7 楼hax(海曦)回复于 2006-03-05 00:02:12 得分 0
上述两种方法在js的文档里都写着。楼主应仔细看文档。Top
8 楼gu1dai(异域苍穹.百年飞行)回复于 2006-03-06 11:07:52 得分 0
sorry.没留意这个slice.
怎么也想不到slice是消除一项,受教了。
slice
king金山软件
n.
薄片, 切片, 一份, 部分, 片段
v.
切(片)
Top
9 楼gu1dai(异域苍穹.百年飞行)回复于 2006-03-06 11:08:18 得分 0
可惜我代码已经变通写好了。哎。Top




