返回到:Prototype – 数组处理
此方法返回数组的新版本,没有任何 null/undefined 值。
语法
array.compact();
返回值
返回一个新数组,没有任何未定义值的空值。
例子
HTML
x
30
30
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 oldArr =['Sam','Justin','Andrew','Dan', null, undefined];
9
alert("Guys in old Arr" );
10
11
oldArr.each(function(item) {
12
alert(item );
13
});
14
var newArr = oldArr.compact();
15
alert("Guys in new Arr" );
16
17
newArr.each(function(item) {
18
alert(item );
19
});
20
}
21
</script>
22
</head>
23
24
<body>
25
<p>Click the button to see the result.</p>
26
<br />
27
<br />
28
<input type = "button" value = "Result" onclick = "showResult();"/>
29
</body>
30
</html>
返回到:Prototype – 数组处理
阅读剩余 81%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8795.html