返回到:Prototype – 数组处理
此方法减少了数组:单元素数组变成了它们的唯一元素,而多元素数组则原封不动地返回。
语法
array.reduce();
返回值
- 如果是单元素数组,就变成了它们唯一的元素。
- 如果它是多元素数组,那么它保持不变。
例子
HTML
x
23
23
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function showResult() {
8
var arr = [10];
9
alert("Reduced Element : " + arr.reduce());
10
11
var arr = [10, 20, 30, 40];
12
alert("Reduced Element : " + arr.reduce().inspect());
13
}
14
</script>
15
</head>
16
17
<body>
18
<p>Click the button to see the result.</p>
19
<br />
20
<br />
21
<input type = "button" value = "Result" onclick = "showResult();"/>
22
</body>
23
</html>
返回到:Prototype – 数组处理
阅读剩余 35%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8771.html