数组过滤
<h1>数组过滤</h1>
<h2>功能描述</h2>
<p>根据指定的过滤函数对数组进行过滤,返回过滤后的数组、被过滤的数组以及相关长度信息。</p>
<h2>输入参数</h2>
<ul>
<li><strong>数组</strong>:<code>&quot;1|2|3|4|5&quot;</code>(分隔符分隔的字符串)</li>
<li><strong>分隔符</strong>(可选):<code>&quot;|&quot;</code>(默认)</li>
<li><strong>过滤函数</strong>(可选):<code>&quot;x=&gt;true&quot;</code>(默认保留所有元素)</li>
</ul>
<h3>过滤函数种类示例</h3>
<ol>
<li>
<p><strong>数值比较</strong>:
- <code>&quot;x=&gt;x&gt;3&quot;</code>:保留大于3的元素
- <code>&quot;x=&gt;parseInt(x)%2===0&quot;</code>:保留偶数
- <code>&quot;x=&gt;x&gt;=1 &amp;&amp; x&lt;=5&quot;</code>:保留1到5之间的元素
- <code>&quot;x=&gt;x===0&quot;</code>:保留等于0的元素
- <code>&quot;x=&gt;x!==NaN&quot;</code>:排除NaN值</p>
</li>
<li>
<p><strong>字符串操作</strong>:
- <code>&quot;x=&gt;x.length&gt;3&quot;</code>:保留长度大于3的字符串
- <code>&quot;x=&gt;x.startsWith(&#039;a&#039;)&quot;</code>:保留以'a'开头的字符串
- <code>&quot;x=&gt;x.includes(&#039;b&#039;)&quot;</code>:保留包含'b'的字符串
- <code>&quot;x=&gt;/^[A-Z]/.test(x)&quot;</code>:保留以大写字母开头的字符串
- <code>&quot;x=&gt;x.match(/\d+/)&quot;</code>:保留包含数字的字符串</p>
</li>
<li>
<p><strong>逻辑组合</strong>:
- <code>&quot;x=&gt;(x&gt;1 &amp;&amp; x&lt;5) || x===10&quot;</code>:保留1到5之间或等于10的元素
- <code>&quot;x=&gt;!(x===&#039;a&#039; || x===&#039;b&#039;)&quot;</code>:排除'a'或'b'
- <code>&quot;x=&gt;(x&gt;0 &amp;&amp; x&lt;10) &amp;&amp; x!==5&quot;</code>:保留0到10之间但不等于5的元素
- <code>&quot;x=&gt;x===&#039;admin&#039; || x===&#039;root&#039;&quot;</code>:保留'admin'或'root'</p>
</li>
<li>
<p><strong>特殊条件</strong>:
- <code>&quot;x=&gt;x===null&quot;</code>:保留null值
- <code>&quot;x=&gt;typeof x===&#039;number&#039;&quot;</code>:保留数值类型
- <code>&quot;x=&gt;x!==undefined&quot;</code>:排除undefined值
- <code>&quot;x=&gt;Array.isArray(x)&quot;</code>:保留数组类型
- <code>&quot;x=&gt;typeof x===&#039;object&#039; &amp;&amp; x!==null&quot;</code>:保留非null对象</p>
</li>
<li>
<p><strong>正则表达式</strong>:
- <code>&quot;x=&gt;/^\d+$/.test(x)&quot;</code>:保留纯数字字符串
- <code>&quot;x=&gt;/^[a-z]+$/i.test(x)&quot;</code>:保留纯字母字符串
- <code>&quot;x=&gt;/^[\w\-.]+@([\w-]+\.)+[\w-]{2,4}$/.test(x)&quot;</code>:保留邮箱格式字符串</p>
</li>
<li>
<p><strong>数组操作</strong>:
- <code>&quot;x=&gt;x.includes(&#039;value&#039;)&quot;</code>:保留包含'value'的数组
- <code>&quot;x=&gt;x.length&gt;0&quot;</code>:保留非空数组
- <code>&quot;x=&gt;x.some(item=&gt;item&gt;10)&quot;</code>:保留包含大于10元素的数组</p>
</li>
<li><strong>对象属性判断</strong>:
- <code>&quot;x=&gt;x.hasOwnProperty(&#039;id&#039;)&quot;</code>:保留包含'id'属性的对象
- <code>&quot;x=&gt;x.status===&#039;active&#039;&quot;</code>:保留状态为'active'的对象
- <code>&quot;x=&gt;x.value&gt;100&quot;</code>:保留值大于100的对象</li>
</ol>
<h2>输出结果</h2>
<ul>
<li><strong>过滤后的数组</strong>:<code>&quot;2|4&quot;</code></li>
<li><strong>被过滤的数组</strong>:<code>&quot;1|3|5&quot;</code></li>
<li><strong>过滤前数组长度</strong>:<code>5</code></li>
<li><strong>过滤后数组长度</strong>:<code>2</code></li>
<li><strong>被过滤数组长度</strong>:<code>3</code></li>
</ul>
<h2>日志记录</h2>
<h3>输入参数</h3>
<p>数组: 1|2|3|4|5
分隔符: |
过滤函数: x=>x%2===0</p>
<ul>
<li>描述:记录输入的数组、分隔符和过滤函数。</li>
</ul>
<h3>处理过程</h3>
<p>过滤后的数组: 2|4
被过滤的数组: 1|3|5
过滤前数组长度=5
过滤后数组长度=2
被过滤数组长度=3</p>
<ul>
<li>描述:记录过滤过程中的关键数据,包括过滤后的数组、被过滤的数组以及相关长度信息。</li>
</ul>
<h3>错误处理</h3>
<ul>
<li><strong>过滤函数创建失败</strong>:
- 日志输出:<code>过滤函数创建失败</code></li>
</ul>
<h2>不同场景下的输出</h2>
<h3>场景 1:</h3>
<ul>
<li><strong>输入参数</strong>:
- 数组:<code>&quot;a|b|c|d&quot;</code>
- 分隔符:<code>&quot;|&quot;</code>
- 过滤函数:<code>&quot;x=&gt;x!==&#039;b&#039;&quot;</code></li>
<li><strong>输出结果</strong>:
- 过滤后的数组:<code>&quot;a|c|d&quot;</code>
- 被过滤的数组:<code>&quot;b&quot;</code>
- 过滤前数组长度:<code>4</code>
- 过滤后数组长度:<code>3</code>
- 被过滤数组长度:<code>1</code></li>
<li><strong>日志输出</strong>:
- 输入参数:
数组: a|b|c|d
分隔符: |
过滤函数: x=>x!=='b'
- 输出结果:
过滤后的数组: a|c|d
被过滤的数组: b
过滤前数组长度=4
过滤后数组长度=3
被过滤数组长度=1</li>
</ul>
<h3>场景 2:</h3>
<ul>
<li><strong>输入参数</strong>:
- 数组:<code>&quot;10|20|30|40|50&quot;</code>
- 分隔符:<code>&quot;|&quot;</code>
- 过滤函数:<code>&quot;x=&gt;x&gt;25&quot;</code></li>
<li><strong>输出结果</strong>:
- 过滤后的数组:<code>&quot;30|40|50&quot;</code>
- 被过滤的数组:<code>&quot;10|20&quot;</code>
- 过滤前数组长度:<code>5</code>
- 过滤后数组长度:<code>3</code>
- 被过滤数组长度:<code>2</code></li>
<li><strong>日志输出</strong>:
- 输入参数:
数组: 10|20|30|40|50
分隔符: |
过滤函数: x=>x>25
- 输出结果:
过滤后的数组: 30|40|50
被过滤的数组: 10|20
过滤前数组长度=5
过滤后数组长度=3
被过滤数组长度=2</li>
</ul>
<h3>场景 3:</h3>
<ul>
<li><strong>输入参数</strong>:
- 数组:<code>&quot;1|2|3|4|5&quot;</code>
- 分隔符:<code>&quot;|&quot;</code>
- 过滤函数:<code>&quot;x=&gt;x%2===0&quot;</code></li>
<li><strong>输出结果</strong>:
- 过滤后的数组:<code>&quot;2|4&quot;</code>
- 被过滤的数组:<code>&quot;1|3|5&quot;</code>
- 过滤前数组长度:<code>5</code>
- 过滤后数组长度:<code>2</code>
- 被过滤数组长度:<code>3</code></li>
<li><strong>日志输出</strong>:
- 输入参数:
数组: 1|2|3|4|5
分隔符: |
过滤函数: x=>x%2===0
- 输出结果:
过滤后的数组: 2|4
被过滤的数组: 1|3|5
过滤前数组长度=5
过滤后数组长度=2
被过滤数组长度=3</li>
</ul>
<h3>场景 4:</h3>
<ul>
<li><strong>输入参数</strong>:
- 数组:<code>&quot;apple|banana|cherry|date&quot;</code>
- 分隔符:<code>&quot;|&quot;</code>
- 过滤函数:<code>&quot;x=&gt;x.length&gt;5&quot;</code></li>
<li><strong>输出结果</strong>:
- 过滤后的数组:<code>&quot;banana|cherry&quot;</code>
- 被过滤的数组:<code>&quot;apple|date&quot;</code>
- 过滤前数组长度:<code>4</code>
- 过滤后数组长度:<code>2</code>
- 被过滤数组长度:<code>2</code></li>
<li><strong>日志输出</strong>:
- 输入参数:
数组: apple|banana|cherry|date
分隔符: |
过滤函数: x=>x.length>5
- 输出结果:
过滤后的数组: banana|cherry
被过滤的数组: apple|date
过滤前数组长度=4
过滤后数组长度=2
被过滤数组长度=2</li>
</ul>
<h3>场景 5:</h3>
<ul>
<li><strong>输入参数</strong>:
- 数组:<code>&quot;true|false|true|false&quot;</code>
- 分隔符:<code>&quot;|&quot;</code>
- 过滤函数:<code>&quot;x=&gt;x===&#039;true&#039;&quot;</code></li>
<li><strong>输出结果</strong>:
- 过滤后的数组:<code>&quot;true|true&quot;</code>
- 被过滤的数组:<code>&quot;false|false&quot;</code>
- 过滤前数组长度:<code>4</code>
- 过滤后数组长度:<code>2</code>
- 被过滤数组长度:<code>2</code></li>
<li><strong>日志输出</strong>:
- 输入参数:
数组: true|false|true|false
分隔符: |
过滤函数: x=>x==='true'
- 输出结果:
过滤后的数组: true|true
被过滤的数组: false|false
过滤前数组长度=4
过滤后数组长度=2
被过滤数组长度=2</li>
</ul>
<h3>场景 6:</h3>
<ul>
<li><strong>输入参数</strong>:
- 数组:<code>&quot;&quot;</code>(空数组)
- 分隔符:<code>&quot;|&quot;</code>
- 过滤函数:<code>&quot;x=&gt;x.length&gt;0&quot;</code></li>
<li><strong>输出结果</strong>:
- 过滤后的数组:<code>&quot;&quot;</code>
- 被过滤的数组:<code>&quot;&quot;</code>
- 过滤前数组长度:<code>0</code>
- 过滤后数组长度:<code>0</code>
- 被过滤数组长度:<code>0</code></li>
<li><strong>日志输出</strong>:
- 输入参数:
数组:
分隔符: |
过滤函数: x=>x.length>0
- 输出结果:
过滤后的数组:
被过滤的数组:
过滤前数组长度=0
过滤后数组长度=0
被过滤数组长度=0</li>
</ul>
<h3>场景 7:</h3>
<ul>
<li><strong>输入参数</strong>:
- 数组:<code>&quot;@|#|$|%&quot;</code>
- 分隔符:<code>&quot;|&quot;</code>
- 过滤函数:<code>&quot;x=&gt;x===&#039;#&#039;&quot;</code></li>
<li><strong>输出结果</strong>:
- 过滤后的数组:<code>&quot;#&quot;</code>
- 被过滤的数组:<code>&quot;@|$|%&quot;</code>
- 过滤前数组长度:<code>4</code>
- 过滤后数组长度:<code>1</code>
- 被过滤数组长度:<code>3</code></li>
<li><strong>日志输出</strong>:
- 输入参数:
数组: @|#|$|%
分隔符: |
过滤函数: x=>x==='#'
- 输出结果:
过滤后的数组: #
被过滤的数组: @|$|%
过滤前数组长度=4
过滤后数组长度=1
被过滤数组长度=3</li>
</ul>
<h3>场景 8:</h3>
<ul>
<li><strong>输入参数</strong>:
- 数组:<code>&quot;1|2|3|4|5&quot;</code>
- 分隔符:<code>&quot;|&quot;</code>
- 过滤函数:<code>&quot;x=&gt;parseInt(x)&gt;2&quot;</code></li>
<li><strong>输出结果</strong>:
- 过滤后的数组:<code>&quot;3|4|5&quot;</code>
- 被过滤的数组:<code>&quot;1|2&quot;</code>
- 过滤前数组长度:<code>5</code>
- 过滤后数组长度:<code>3</code>
- 被过滤数组长度:<code>2</code></li>
<li><strong>日志输出</strong>:
- 输入参数:
数组: 1|2|3|4|5
分隔符: |
过滤函数: x=>parseInt(x)>2
- 输出结果:
过滤后的数组: 3|4|5
被过滤的数组: 1|2
过滤前数组长度=5
过滤后数组长度=3
被过滤数组长度=2</li>
</ul>