原型 - 可列舉 grep() 方法
此方法返回與 filter 匹配的所有元素。如果提供了迭代器,則使用它針對每個選定的元素生成返回值。
可選的 iterator 引數將以類似於 map() 方法的方式轉換結果集。
可選的 context 引數是迭代器函式將繫結到的引數。如果使用,則迭代器中的 this 關鍵字將指向引數給出的物件。
語法
Iterator.findAll();
返回值
返回所有迭代器返回 true 的元素。
示例
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
// Get all strings with a repeated letter somewhere
alert(['hello', 'world', 'is', 'cool'].grep(/(.)\1/).inspect());
// Returns ['hello', 'cool']
// Get all numbers ending with 0 or 5
alert($R(1,30).grep(/[05]$/).inspect() );
// Returns [5, 10, 15, 20, 25, 30]
}
</script>
</head>
<body>
<p>Click the button to see the result.</p>
<br />
<br />
<input type = "button" value = "Result" onclick = "showResult();"/>
</body>
</html>
輸出
prototype_enumerating.htm
廣告