Prototype - Enumerable partition() 方法
此方法将元素分成两组 −
- 那些被视为 true。
- 那些被视为 false。
默认情况下,使用常规 JavaScript 布尔等价,但可以提供迭代器,计算元素的布尔表示。
这是使用 findAll/select 和 rejection 的首选解决方案:它只循环遍历元素一次!
可选的 context 参数是迭代器函数将绑定到的内容。如果使用,迭代器内的 this 关键字将指向参数给出的对象。
语法
Iterator.partition([context]);
返回值
返回一个数组,格式为 [[真值数组], [假值数组]]。
示例
<html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var arr = ['hello', null, 42, false, true, , 17].partition(); alert("Test 1 : " + arr.inspect() ); // Returns [['hello', 42, true, 17], [null, false, undefined]] var arr = $R(1, 10).partition(function(n) { return 0 == n % 2; }) alert("Test 2 : " + arr.inspect() ); // Returns [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]] } </script> </head> <body> <p>单击按钮查看结果。</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html>