js如何动态添加数组?

js如何动态添加数组?,第1张

js动态添加数组可以按下面的步骤:

1、在数组的开头添加新元素 - unshift()

源代码:

<!DOCTYPE html>

<html>

<body>

<p id="demo">Click the button to add elements to the array.</p>

<button onclick="myFunction()">Try it</button>

<script>

function myFunction()

{

var fruits = ["Banana", "Orange", "Apple", "Mango"]

fruits.unshift("Lemon","Pineapple")

var x=document.getElementById("demo")

x.innerHTML=fruits

}

</script>

<p><b>Note:</b>The unshift() method does not work properly in Internet Explorer 8 and earlier, the values will be inserted, but the return value will be <em>undefined</em>.</p>

</body>

</html>

测试结果:

Lemon,Pineapple,Banana,Orange,Apple,Mango

2、在数组的第2位置添加一个元素 - splice()

源代码:

<!DOCTYPE html>

<html>

<body>

<p id="demo">Click the button to add elements to the array.</p>

<button onclick="myFunction()">Try it</button>

<script>

function myFunction()

{

var fruits = ["Banana", "Orange", "Apple", "Mango"]

fruits.splice(2,0,"Lemon","Kiwi")

var x=document.getElementById("demo")

x.innerHTML=fruits

}

</script>

</body>

</html> 

测试结果:

Banana,Orange,Lemon,Kiwi,Apple,Mango

3、数组的末尾添加新的元素 - push()

源代码:

<!DOCTYPE html>

<html>

<body>

<p id="demo">Click the button to add a new element to the array.</p>

<button onclick="myFunction()">Try it</button>

<script>

var fruits = ["Banana", "Orange", "Apple", "Mango"]

function myFunction()

{

fruits.push("Kiwi")

var x=document.getElementById("demo")

x.innerHTML=fruits

}

</script>

</body>

</html>

测试结果:

Banana,Orange,Apple,Mango,Kiwi

JavaScript中,由于数组长度是可变的,因此可以通过直接定义新的成员而将其添加到数组中:

var o = [2,3,5]

o[3] = 7

console.log(o)//[2,3,5,7]

除了这种方法,还可以通过使用push()语句来达到相同的目的:

o.push(11)

console.log(o)//[2,3,5,7,11]

o.push(13,17)

console.log(o)//[2,3,5,7,11,13,17]

如果需要在数组开头添加新的成员,可以使用unshift()语句:

o.unshift(2014)

console.log(o)//[2014,2,3,5,7,11,13,17]

o.unshift(2013, 2012)

console.log(o)//[2013,2012,2014, 2,3,5,7,11,13,17]

与push()对应,如果需要从数组末尾删除一个成员,可以使用pop()语句,pop()语句将返回这个被删除的成员,而数组长度将减少1:

var p = o.pop()

console.log(p)//17

console.log(o.length)//9

与unshift()对应,如果需要从数组开头删除一个成员,可以使用shift()语句,shift()语句将返回这个被删除的成员,而数组长度将减少1:

var s = o.shift()

console.log(s)//2013

console.log(o.length)//8

除了shift()语句和pop()语句,还可以通过delete *** 作符来删除数组中的成员。与shift()和pop()不同的是,delete *** 作后数组的length属性将保持不变,也即数组将变得不连续。

JavaScript中还可以通过设定数组的length属性来对数组进行修改:当length值小于数组成员数时,JavaScript将对数组进行截取;当length值大于数组成员数时,JavaScript会将数组变得不连续。如果length值只读,那么在数组中直接定义新成员的 *** 作将会失败:

console.log(o)//[2012,2014, 2,3,5,7,11,13]

o.length = 2

console.log(o)//[2012,2014]

o.length = 4

console.log(o)//[2012,2014,undefined,undefined]

var a = [1,2,3]

Object.defineProperty(a, "length", {writable:false})

a[3] = 4

console.log(a)//[1,2,3]

1.ArrayList方法摘要

构造方法摘要

ArrayList()

构造一个初始容量为 10 的空列表。

ArrayList(Collection<? extends E>c)

构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。

ArrayList(int initialCapacity)

构造一个具有指定初始容量的空列表。

方法摘要

boolean add(E e)

将指定的元素添加到此列表的尾部。

void add(int index, E element)

将指定的元素插入此列表中的指定位置。

boolean addAll(Collection<? extends E>c)

按照指定 collection 的迭代器所返回的元素顺序,将该 collection 中的所有元素添加到此列表的尾部。

boolean addAll(int index, Collection<? extends E>c)

从指定的位置开始,将指定 collection 中的所有元素插入到此列表中。

void clear()

移除此列表中的所有元素。

Object clone()

返回此 ArrayList 实例的浅表副本。

boolean contains(Object o)

如果此列表中包含指定的元素,则返回 true。

void ensureCapacity(int minCapacity)

如有必要,增加此 ArrayList 实例的容量,以确保它至少能够容纳最小容量参数所指定的元素数。

E get(int index)

返回此列表中指定位置上的元素。

int indexOf(Object o)

返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。

boolean isEmpty()

如果此列表中没有元素,则返回 true

int lastIndexOf(Object o)

返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。

E remove(int index)

移除此列表中指定位置上的元素。

boolean remove(Object o)

移除此列表中首次出现的指定元素(如果存在)。

protected void removeRange(int fromIndex, int toIndex)

移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。

E set(int index, E element)

用指定的元素替代此列表中指定位置上的元素。

int size()

返回此列表中的元素数。

Object[] toArray()

按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。

<T>T[] toArray(T[] a)

按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。

void trimToSize()

将此 ArrayList 实例的容量调整为列表的当前大小。

2.js实现部分功能

复制代码 代码如下:

<html>

<script type="text/javascript" src="json.js"></script>

<head>

<script type="text/javascript">

function ArrayList(){

this.arr=[],

this.size=function(){

return this.arr.length

},

this.add=function(){

if(arguments.length==1){

this.arr.push(arguments[0])

}else if(arguments.length>=2){

var deleteItem=this.arr[arguments[0]]

this.arr.splice(arguments[0],1,arguments[1],deleteItem)

}

return this

},

this.get=function(index){

return this.arr[index]

},

this.removeIndex=function(index){

this.arr.splice(index,1)

},

this.removeObj=function(obj){

this.removeIndexwww.bjldfw.comindexOf(obj))

},

this.indexOf=function(obj){

for(var i=0i<this.arr.lengthi++){

if www.bjldfw.comarr[i]===obj) {

return i

}

}

return -1

},

this.isEmpty=function(){

return this.arr.length==0

},

this.clear=function(){

this.arr=[]

},

this.contains=function(obj){

return this.indexOf(obj)!=-1

}

}

//新建一个List

var list=new ArrayList()

//增加一个元素

list.add("0").add("1").add("2").add("3")

//增加指定位置

list.add(2,"22222222222")

//删除指定元素

list.removeObj("3")

//删除指定位置元素

list.removeIndex(0)

for(var i=0i<list.size()i++){

document.writeln(list.get(i))

}

document.writeln(list.contains("2"))

</script>

</head>

<body>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/7941949.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存