首页 > 安全资讯 >

Array(数组)对象

16-01-26

源代码 运行结果

创建数组
源代码
<!DOCTYPE html>
<html>
<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>
<body>

<p id="demo">点击按钮合并数组。</p>

<button onclick="myFunction()">点我</button>

<script>
function myFunction()
{
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);
var x=document.getElementById("demo");
x.innerHTML=children;
}
</script>

</body>
</html>

 

运行结果
合并三个数组 - concat()
源代码
<!DOCTYPE html>
<html>
<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>菜鸟教程(runoob.com)</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>
<body>

<p id="demo">Click the button to remove the last array element.</p>

<button onclick="myFunction()">Try it</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>
<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>

 

运行结果
反转一个数组中的元素的顺序 - reverse()
源代码
<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to reverse the order of the elements in the array.</p>

<button onclick="myFunction()">Try it</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>
<body>

<p id="demo">Click the button to remove the first element of the array.</p>

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

<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];

function myFunction()
{
fruits.shift();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>

</body>
</html>

 

运行结果
从一个数组中的选择元素 - slice()
源代码
<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to extract the second and the third elements from the array.</p>

<button onclick="myFunction()">Try it</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>
<body>

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

<button onclick="myFunction()">Try it</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>
<body>

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

<button onclick="myFunction()">Try it</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>
<body>

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

<button onclick="myFunction()">Try it</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>
<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>

 

运行结果
转换数组到字符串 -toString()
源代码
<!DOCTYPE html>
<html>
<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>
<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>

 

运行结果

相关文章
最新文章
热点推荐