aboutsummaryrefslogtreecommitdiff
path: root/files/vi/web/javascript/reference/global_objects/array/findindex
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
commit218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch)
treea9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/vi/web/javascript/reference/global_objects/array/findindex
parent074785cea106179cb3305637055ab0a009ca74f2 (diff)
downloadtranslated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip
initial commit
Diffstat (limited to 'files/vi/web/javascript/reference/global_objects/array/findindex')
-rw-r--r--files/vi/web/javascript/reference/global_objects/array/findindex/index.html207
1 files changed, 207 insertions, 0 deletions
diff --git a/files/vi/web/javascript/reference/global_objects/array/findindex/index.html b/files/vi/web/javascript/reference/global_objects/array/findindex/index.html
new file mode 100644
index 0000000000..3c8d822f07
--- /dev/null
+++ b/files/vi/web/javascript/reference/global_objects/array/findindex/index.html
@@ -0,0 +1,207 @@
+---
+title: Array.prototype.findIndex()
+slug: Web/JavaScript/Reference/Global_Objects/Array/findIndex
+translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex
+---
+<div>{{JSRef}}</div>
+
+<p>Phương thức <code><strong>findIndex()</strong></code> trả về <strong>chỉ số (index)</strong> của phần tử đầu tiên trong mảng thỏa mãn hàm truyền vào. Nếu không phần tử nào thỏa mãn, phương thức trả lại -1.</p>
+
+<pre class="brush: js">function isBigEnough(element) {
+ return element &gt;= 15;
+}
+
+[12, 5, 8, 130, 44].findIndex(isBigEnough); // Trả về 3, phần tử thứ 4.</pre>
+
+<p>Xem thêm phương thức {{jsxref("Array.find", "find()")}}, trả về <strong>giá trị (value)</strong> của phần tử được tìm thấy thay vì chỉ số.</p>
+
+<h2 id="Cú_pháp">Cú pháp</h2>
+
+<pre class="syntaxbox"><var>arr</var>.findIndex(<var>callback</var>[, <var>thisArg</var>])</pre>
+
+<h3 id="Tham_số">Tham số</h3>
+
+<dl>
+ <dt><code>callback</code></dt>
+ <dd>Hàm kiểm tra, được thực thi cho mỗi phần tử của mảng, có thể chứa 3 tham số:
+ <dl>
+ <dt><code>element</code></dt>
+ <dd>Phần tử đang xét.</dd>
+ <dt><code>index</code></dt>
+ <dd>Chỉ số của phần tử đang xét.</dd>
+ <dt><code>array</code></dt>
+ <dd>Mảng được gọi.</dd>
+ </dl>
+ </dd>
+ <dt><code>thisArg</code></dt>
+ <dd>Optional. Object to use as <code>this</code> when executing <code>callback</code>.</dd>
+</dl>
+
+<h3 id="Giá_trị_trả_về">Giá trị trả về</h3>
+
+<p>Một chỉ số của mảng nếu tìm được phần tử đầu tiên thỏa mãn hàm kiểm tra; otherwise, <strong>-1</strong>.</p>
+
+<h2 id="Mô_tả">Mô tả</h2>
+
+<p>Phương thức <code>findIndex</code> thực thi hàm <code>callback</code> từng lần cho toàn bộ chỉ số mảng từ  <code>0..length-1</code> (bao gồm hai nút) trong mảng cho tới khi tìm thấy chỉ số mà phần tử tại đó làm cho <code>callback</code> trả về một giá trị đúng (a value that coerces to <code>true</code>). Nếu một phần tử được tìm thấy, <code>findIndex</code> lập tức trả về chỉ số của phần tử này. Nếu hàm callback luôn nhận giá trị không đúng hoặc số phần tử của mảng bằng 0, <code>findIndex</code> trả về -1. Không giống như cách phương thức về mảng khác như Array#some, trong mảng thưa thớt hàm <code>callback</code> được gọi là ngay cả đối với các chỉ mục của mục không có trong mảng đó.</p>
+
+<p><code>callback</code> được gọi với 3 đối số: giá trị của phần tử, chỉ số của phần tử, và mảng đang được xét.</p>
+
+<p>Nếu tham số <code>thisArg</code> được đưa vào <code>findIndex</code>, Nó sẽ được sử dụng như là <code>this</code> cho mỗi lần gọi <code>callback</code>. Nếu nó không được đưa vào, thì {{jsxref("undefined")}} sẽ được sử dụng.</p>
+
+<p><code>findIndex</code> không làm thay đổi mảng mà nó được gọi.</p>
+
+<p>The range of elements processed by <code>findIndex</code> is set before the first invocation of <code>callback</code>. Elements that are appended to the array after the call to <code>findIndex</code> begins will not be visited by <code>callback</code>. If an existing, unvisited element of the array is changed by <code>callback</code>, its value passed to the visiting <code>callback</code> will be the value at the time that <code>findIndex</code> visits that element's index; elements that are deleted are not visited.</p>
+
+<h2 id="Ví_dụ">Ví dụ</h2>
+
+<h3 id="Tìm_chỉ_số_của_một_số_nguyên_tố_trong_mảng">Tìm chỉ số của một số nguyên tố trong mảng</h3>
+
+<p>Ví dụ dưới đây tìm chỉ số của một phần tử trong mảng mà là số nguyên tố (trả về -1 nếu không tìm thấy).</p>
+
+<pre class="brush: js">function isPrime(element, index, array) {
+ var start = 2;
+ while (start &lt;= Math.sqrt(element)) {
+ if (element % start++ &lt; 1) {
+ return false;
+ }
+ }
+ return element &gt; 1;
+}
+
+console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found
+console.log([4, 6, 7, 12].findIndex(isPrime)); // 2
+</pre>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<pre class="brush: js">// https://tc39.github.io/ecma262/#sec-array.prototype.findIndex
+if (!Array.prototype.findIndex) {
+ Object.defineProperty(Array.prototype, 'findIndex', {
+ value: function(predicate) {
+ // 1. Let O be ? ToObject(this value).
+ if (this == null) {
+ throw new TypeError('"this" is null or not defined');
+ }
+
+ var o = Object(this);
+
+ // 2. Let len be ? ToLength(? Get(O, "length")).
+ var len = o.length &gt;&gt;&gt; 0;
+
+ // 3. If IsCallable(predicate) is false, throw a TypeError exception.
+ if (typeof predicate !== 'function') {
+ throw new TypeError('predicate must be a function');
+ }
+
+ // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
+ var thisArg = arguments[1];
+
+ // 5. Let k be 0.
+ var k = 0;
+
+ // 6. Repeat, while k &lt; len
+ while (k &lt; len) {
+ // a. Let Pk be ! ToString(k).
+ // b. Let kValue be ? Get(O, Pk).
+ // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
+ // d. If testResult is true, return k.
+ var kValue = o[k];
+ if (predicate.call(thisArg, kValue, k, o)) {
+ return k;
+ }
+ // e. Increase k by 1.
+ k++;
+ }
+
+ // 7. Return -1.
+ return -1;
+ }
+ });
+}
+</pre>
+
+<p>If you need to support truly obsolete JavaScript engines that don't support <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code>, it's best not to polyfill <code>Array.prototype</code> methods at all, as you can't make them non-enumerable.</p>
+
+<h2 id="Các_thông_số">Các thông số</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES2015', '#sec-array.prototype.findindex', 'Array.prototype.findIndex')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-array.prototype.findIndex', 'Array.prototype.findIndex')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Tương_thích_trình_duyệt">Tương thích trình duyệt</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Edge</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatChrome(45.0)}}</td>
+ <td>{{CompatGeckoDesktop("25.0")}}</td>
+ <td>{{CompatNo}}</td>
+ <td>Yes</td>
+ <td>Yes</td>
+ <td>{{CompatSafari("7.1")}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Chrome for Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatGeckoMobile("25.0")}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>8.0</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="Xem_thêm">Xem thêm</h2>
+
+<ul>
+ <li>{{jsxref("Array.prototype.find()")}}</li>
+ <li>{{jsxref("Array.prototype.indexOf()")}}</li>
+</ul>