<html>
<head>
<title>用slice方法返回数组一部分的Javascript示例</title>
</head>
<body>
<script type="text/javascript">
var a;
a = new Array("a","b","c","d","e");
document.write(a.slice(0,3) + "<br>");
document.write(a.slice(-3,4) + "<br>");
document.write(a.slice(0,-1) + "<br>");
document.write(a.slice(1) + "<br>");
document.write(a.slice(4,3));
</script>
<p>该Javascript示例解释:这个示例用到了数组(Array)对象的Slice(start,[end])方法,该方法返回一个新数组,包含了源函数从start到 end 所指定的元素,但是不包括end元素,比如a.slice(0,3);如果 start 为负,将它作为 length + start处理,此处 length 为数组的长度,比如a.slice(-3,4),相当于a.slice(2,4)。如果 end 为负,就将它作为 length + end 处理,此处 length 为数组的长度,比如a.slice(0,-1)。如果省略 end ,那么 slice 方法将一直复制到源数组结尾,比如a.slice(1)。如果 end 出现在 start 之前,不复制任何元素到新数组中,比如a.slice(4,3)。</p>
</body>
</html>
JavaScript join() Method
Definition and Usage
The join() method is used to put all the elements of an array into a string.
The elements will be separated by a specified separator
Tips and Notes
Note: Comma is the default separator for the join() method.
Example
In this example we will create an array, and then put all the elements in a string:
<script type="text/javascript"> var arr = new Array(3); arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; document.write(arr.join() + "<br />"); document.write(arr.join(".")); </script> |
The output of the code above will be:
Jani,Hege,Stale Jani.Hege.Stale |