Categories: Prototype Api

Prototype – 可枚举的 partition() 方法

返回到:Prototype – 枚举

这种方法将元素分成两组:

  • 那些被认为是真实的。
  • 那些被认为是错误的。

默认情况下,使用常规 JavaScript 布尔等价,但可以提供迭代器,计算元素的布尔表示。

这是同时使用 findAll/select 和 reject 的首选解决方案:它只循环遍历元素一次!

可选的上下文参数是迭代器函数将绑定到的内容。如果使用,迭代器中的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>Click the button to see the result.</p>
      <br />
      <br />
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

返回到:Prototype – 枚举

terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

vue:页面注入js修改input值

一般会直接这样写: let z…

18 小时 ago

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago