mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-16 07:51:52 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<p>另请参见 <a href="Reference/Global_Objects/Array/findIndex" title="findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。"><code>findIndex()</code></a> 方法,它返回数组中找到的元素的索引,而不是其值。</p>
|
||||
<p>如果你需要找到一个元素的位置或者一个元素是否存在于数组中,使用<a href="Reference/Global_Objects/Array/indexOf" title="indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。"><code>Array.prototype.indexOf()</code></a> 或 <a href="Reference/Global_Objects/Array/includes" title="includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。"><code>Array.prototype.includes()</code></a>。</p>
|
||||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||||
<pre class="syntaxbox"><code><em>arr</em>.find(<em>callback</em>[, <em>thisArg</em>])</code></pre>
|
||||
<pre><code class="language-javascript"><code><em>arr</em>.find(<em>callback</em>[, <em>thisArg</em>])</code></code></pre>
|
||||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||||
<dl>
|
||||
<dt><code>callback</code></dt>
|
||||
@@ -32,7 +32,7 @@
|
||||
<p>在第一次调用 <code>callback</code>函数时会确定元素的索引范围,因此在 <code>find</code>方法开始执行之后添加到数组的新元素将不会被 <code>callback</code>函数访问到。如果数组中一个尚未被<code>callback</code>函数访问到的元素的值被<code>callback</code>函数所改变,那么当<code>callback</code>函数访问到它时,它的值是将是根据它在数组中的索引所访问到的当前值。被删除的元素仍旧会被访问到。</p>
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="用对象的属性查找数组里的对象">用对象的属性查找数组里的对象</h3>
|
||||
<pre class="brush: js">var inventory = [
|
||||
<pre><code class="language-javascript">var inventory = [
|
||||
{name: 'apples', quantity: 2},
|
||||
{name: 'bananas', quantity: 0},
|
||||
{name: 'cherries', quantity: 5}
|
||||
@@ -42,10 +42,10 @@ function findCherries(fruit) {
|
||||
return fruit.name === 'cherries';
|
||||
}
|
||||
|
||||
console.log(inventory.find(findCherries)); // { name: 'cherries', quantity: 5 }</pre>
|
||||
console.log(inventory.find(findCherries)); // { name: 'cherries', quantity: 5 }</code></pre>
|
||||
<h3 id="寻找数组中的质数">寻找数组中的质数</h3>
|
||||
<p>下面的例子展示了如何从一个数组中寻找质数(如果找不到质数则返回<a href="Reference/Global_Objects/undefined" title="undefined是全局对象的一个属性。也就是说,它是全局作用域的一个变量。undefined的最初值就是原始数据类型undefined。"><code>undefined</code></a>)</p>
|
||||
<pre class="brush: js">function isPrime(element, index, array) {
|
||||
<pre><code class="language-javascript">function isPrime(element, index, array) {
|
||||
var start = 2;
|
||||
while (start <= Math.sqrt(element)) {
|
||||
if (element % start++ < 1) {
|
||||
@@ -58,9 +58,9 @@ console.log(inventory.find(findCherries)); // { name: 'cherries', quantity: 5 }<
|
||||
console.log([4, 6, 8, 12].find(isPrime)); // undefined, not found
|
||||
console.log([4, 5, 8, 12].find(isPrime)); // 5
|
||||
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>当在回调中删除数组中的一个值时,当访问到这个位置时,其传入的值时 undefined:</p>
|
||||
<pre class="brush: js">// Declare array with no element at index 2, 3 and 4
|
||||
<pre><code class="language-javascript">// Declare array with no element at index 2, 3 and 4
|
||||
var a = [0,1,,,,5,6];
|
||||
|
||||
// Shows all indexes, not just those that have been assigned values
|
||||
@@ -78,10 +78,10 @@ a.find(function(value, index) {
|
||||
}
|
||||
// Element 5 is still visited even though deleted
|
||||
console.log('Visited index ' + index + ' with value ' + value);
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
|
||||
<p>本方法在ECMAScript 6规范中被加入,可能不存在于某些实现中。你可以通过以下代码来补充 <code>Array.prototype.find()</code>。</p>
|
||||
<pre class="brush: js">// https://tc39.github.io/ecma262/#sec-array.prototype.find
|
||||
<pre><code class="language-javascript">// https://tc39.github.io/ecma262/#sec-array.prototype.find
|
||||
if (!Array.prototype.find) {
|
||||
Object.defineProperty(Array.prototype, 'find', {
|
||||
value: function(predicate) {
|
||||
@@ -124,7 +124,7 @@ if (!Array.prototype.find) {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
}</pre>
|
||||
}</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user