From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../global_objects/array/findindex/index.html | 207 +++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 files/vi/web/javascript/reference/global_objects/array/findindex/index.html (limited to 'files/vi/web/javascript/reference/global_objects/array/findindex') 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 +--- +
{{JSRef}}
+ +

Phương thức findIndex() trả về chỉ số (index) 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.

+ +
function isBigEnough(element) {
+  return element >= 15;
+}
+
+[12, 5, 8, 130, 44].findIndex(isBigEnough); // Trả về 3, phần tử thứ 4.
+ +

Xem thêm phương thức {{jsxref("Array.find", "find()")}}, trả về giá trị (value) của phần tử được tìm thấy thay vì chỉ số.

+ +

Cú pháp

+ +
arr.findIndex(callback[, thisArg])
+ +

Tham số

+ +
+
callback
+
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ố: +
+
element
+
Phần tử đang xét.
+
index
+
Chỉ số của phần tử đang xét.
+
array
+
Mảng được gọi.
+
+
+
thisArg
+
Optional. Object to use as this when executing callback.
+
+ +

Giá trị trả về

+ +

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, -1.

+ +

Mô tả

+ +

Phương thức findIndex thực thi hàm callback từng lần cho toàn bộ chỉ số mảng từ  0..length-1 (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 callback trả về một giá trị đúng (a value that coerces to true). Nếu một phần tử được tìm thấy, findIndex 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, findIndex 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 callback được gọi là ngay cả đối với các chỉ mục của mục không có trong mảng đó.

+ +

callback đượ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.

+ +

Nếu tham số thisArg được đưa vào findIndex, Nó sẽ được sử dụng như là this cho mỗi lần gọi callback. Nếu nó không được đưa vào, thì {{jsxref("undefined")}} sẽ được sử dụng.

+ +

findIndex không làm thay đổi mảng mà nó được gọi.

+ +

The range of elements processed by findIndex is set before the first invocation of callback. Elements that are appended to the array after the call to findIndex begins will not be visited by callback. If an existing, unvisited element of the array is changed by callback, its value passed to the visiting callback will be the value at the time that findIndex visits that element's index; elements that are deleted are not visited.

+ +

Ví dụ

+ +

Tìm chỉ số của một số nguyên tố trong mảng

+ +

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).

+ +
function isPrime(element, index, array) {
+  var start = 2;
+  while (start <= Math.sqrt(element)) {
+    if (element % start++ < 1) {
+      return false;
+    }
+  }
+  return element > 1;
+}
+
+console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found
+console.log([4, 6, 7, 12].findIndex(isPrime)); // 2
+
+ +

Polyfill

+ +
// 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 >>> 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 < len
+      while (k < 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;
+    }
+  });
+}
+
+ +

If you need to support truly obsolete JavaScript engines that don't support Object.defineProperty, it's best not to polyfill Array.prototype methods at all, as you can't make them non-enumerable.

+ +

Các thông số

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-array.prototype.findindex', 'Array.prototype.findIndex')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-array.prototype.findIndex', 'Array.prototype.findIndex')}}{{Spec2('ESDraft')}} 
+ +

Tương thích trình duyệt

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerEdgeOperaSafari
Basic support{{CompatChrome(45.0)}}{{CompatGeckoDesktop("25.0")}}{{CompatNo}}YesYes{{CompatSafari("7.1")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile("25.0")}}{{CompatNo}}{{CompatNo}}8.0
+
+ +

Xem thêm

+ + -- cgit v1.2.3-54-g00ecf