文章目录
JavaScript Array 数组对象实例集合 创建数组 合并两个数组 - concat() 合并三个数组 - concat() 用数组的元素组成字符串 - join() 删除数组的最后一个元素 - pop() 数组的末尾添加新的元素 - push() 反转一个数组中的元素的顺序 - reverse() 删除数组的第一个元素 - shift() 从一个数组中的选择元素 - slice() 数组排序(按字母顺序升序)- sort() 数字排序(按数字顺序升序)- sort() 数字排序(按数字顺序降序)- sort() 在数组的第2位置添加一个元素 - splice() 转换数组到字符串 -toString() 在数组的开头添加新元素 - unshift()
JavaScript Array 数组对象实例集合
创建数组
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< script>
var i;
var mycars = new Array ( ) ;
mycars[ 0 ] = "Saab" ;
mycars[ 1 ] = "Volvo" ;
mycars[ 2 ] = "BMW" ;
for ( i= 0 ; i< mycars. length; i++ ) {
document. write ( mycars[ i] + "<br>" ) ;
}
</ script>
</ body>
</ html>
运行效果
合并两个数组 - concat()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< script>
var hege = [ "Cecilie" , "Lone" ] ;
var stale = [ "Emil" , "Tobias" , "Linus" ] ;
var kai = [ "Robin" ] ;
var children = hege. concat ( stale, kai) ;
document. write ( children) ;
</ script>
</ body>
</ html>
运行效果
合并三个数组 - concat()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< script>
var parents = [ "Jani" , "Tove" ] ;
var brothers = [ "Stale" , "Kai Jim" , "Borge" ] ;
var children = [ "Cecilie" , "Lone" ] ;
var family = parents. concat ( brothers, children) ;
document. write ( family) ;
</ script>
</ body>
</ html>
运行效果
用数组的元素组成字符串 - join()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 点击按钮将数组作为字符串输出。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
function myFunction ( ) {
var fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ] ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= fruits. join ( ) ;
}
</ script>
</ body>
</ html>
运行效果
删除数组的最后一个元素 - pop()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 单击按钮删除数组的最后一个元素。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
var fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ] ;
function myFunction ( ) {
fruits. pop ( ) ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= fruits;
}
</ script>
</ body>
</ html>
数组的末尾添加新的元素 - push()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 单击按钮给数组添加新的元素。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
var fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ] ;
function myFunction ( ) {
fruits. push ( "Kiwi" )
var x= document. getElementById ( "demo" ) ;
x. innerHTML= fruits;
}
</ script>
</ body>
</ html>
反转一个数组中的元素的顺序 - reverse()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 单击按钮将数组反转排序。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
var fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ] ;
function myFunction ( ) {
fruits. reverse ( ) ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= fruits;
}
</ script>
</ body>
</ html>
删除数组的第一个元素 - shift()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 单击按钮删除数组的第一个元素。</ p>
< p id = " demo2" > </ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
var fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ] ;
function myFunction ( ) {
var delell = fruits. shift ( ) ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= '删除后数组为:' + fruits;
document. getElementById ( "demo2" ) . innerHTML= '删除的元素是:' + delell;
}
</ script>
</ body>
</ html>
从一个数组中的选择元素 - slice()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 点击按钮截取数组下标 1 到 2 的元素。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
function myFunction ( ) {
var fruits = [ "Banana" , "Orange" , "Lemon" , "Apple" , "Mango" ] ;
var citrus = fruits. slice ( 1 , 3 ) ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= citrus;
}
</ script>
</ body>
</ html>
运行效果
数组排序(按字母顺序升序)- sort()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 单击按钮升序排列数组。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
function myFunction ( ) {
var fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ] ;
fruits. sort ( ) ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= fruits;
}
</ script>
</ body>
</ html>
运行效果
数字排序(按数字顺序升序)- sort()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 单击按钮升序排列数组。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
function myFunction ( ) {
var points = [ 40 , 100 , 1 , 5 , 25 , 10 ] ;
points. sort ( function ( a, b ) { return a- b} ) ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= points;
}
</ script>
</ body>
</ html>
运行效果
数字排序(按数字顺序降序)- sort()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 单击按钮降序排列数组。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
function myFunction ( ) {
var points = [ 40 , 100 , 1 , 5 , 25 , 10 ] ;
points. sort ( function ( a, b ) { return b- a} ) ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= points;
}
</ script>
</ body>
</ html>
运行效果
在数组的第2位置添加一个元素 - splice()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 点击按钮向数组添加元素。</ p>
< button onclick = " myFunction ( ) " > 点我</ 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>
运行效果
转换数组到字符串 -toString()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 点击按钮将数组转为字符串并返回。</ p>
< button onclick = " myFunction ( ) " > 点我</ button>
< script>
function myFunction ( ) {
var fruits = [ "Banana" , "Orange" , "Apple" , "Mango" ] ;
var str = fruits. toString ( ) ;
var x= document. getElementById ( "demo" ) ;
x. innerHTML= str;
}
</ script>
</ body>
</ html>
运行效果
在数组的开头添加新元素 - unshift()
<! DOCTYPE html >
< html>
< head>
< meta charset = " utf-8" >
< title> 梁辰兴实例</ title>
</ head>
< body>
< p id = " demo" > 单击按钮在数组中插入元素。</ p>
< button onclick = " myFunction ( ) " > 点我</ 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> 注意:</ b> unshift()方法不能用于 Internet Explorer 8 之前的版本,插入的值将被返回成< em> undefined </ em> 。</ p>
</ body>
</ html>
运行效果