This article describes the common usage of JS arrays. Share it for your reference. The specific methods are as follows:
Copy the code as follows:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Application of array method</title>
<script type="text/javascript">
var arrayFruit = new Array("apple", "orange", "peach", "bannaner");
document.write("Array before sorting<br/>");
for(var i in arrayFruit) {
document.write(arrayFruit[i] + "<br />");
}
arrayFruit.sort(); //Sort the array
document.write("Sorted array:<br />");
for(var i in arrayFruit) {
document.write(arrayFruit[i] + "<br />");
}
document.write("Sorted strings connected:<br />");
var str = arrayFruit.join("-");
document.write(str);
var strFruit = "apple,bannaner,orange,peach";
var tempFruit = strFruit.split(",");
document.write("Array divided by string<br />");
for(var i in tempFruit) {
document.write(tempFruit[i] + "<br />");
}
</script>
</head>
<body>
</body>
</html>
It mainly introduces the traversal of arrays and the segmentation of strings. Interested friends can test and run them.
I hope this article will be helpful to everyone's JavaScript programming.