diff options
Diffstat (limited to 'files/vi/web/javascript/reference/global_objects/array/some')
| -rw-r--r-- | files/vi/web/javascript/reference/global_objects/array/some/index.html | 206 |
1 files changed, 0 insertions, 206 deletions
diff --git a/files/vi/web/javascript/reference/global_objects/array/some/index.html b/files/vi/web/javascript/reference/global_objects/array/some/index.html deleted file mode 100644 index bbc279dc5c..0000000000 --- a/files/vi/web/javascript/reference/global_objects/array/some/index.html +++ /dev/null @@ -1,206 +0,0 @@ ---- -title: Array.prototype.some() -slug: Web/JavaScript/Reference/Global_Objects/Array/some -tags: - - ECMAScript 5 - - JavaScript - - Mảng - - Phương Thức - - Prototype - - Tham khảo -translation_of: Web/JavaScript/Reference/Global_Objects/Array/some ---- -<div>{{JSRef}}</div> - -<p>Phương thức <code><strong>some()</strong></code> kiểm tra xem có ít nhất một phần tử của mảng thoả điều kiện ở hàm được truyền vào hay không. Kết quả trả về có kiểu <code>Boolean</code>. </p> - -<div class="note"> -<p><strong>Chú ý</strong>: Phương thức này sẽ trả về <code>false</code> nếu mảng rỗng.</p> -</div> - -<div>{{EmbedInteractiveExample("pages/js/array-some.html")}}</div> - - - -<h2 id="Cú_pháp">Cú pháp</h2> - -<pre class="syntaxbox"><var>arr</var>.some(<var>callback(element[, index[, array]])</var>[, <var>thisArg</var>])</pre> - -<h3 id="Các_tham_số">Các tham số</h3> - -<dl> - <dt><code>callback</code></dt> - <dd>Hàm dùng để kiểm tra từng phần tử, nhận vào ba đối số: - <dl> - <dt><code>element</code></dt> - <dd>Phần tử đang được kiểm tra.</dd> - <dt><code>index</code> {{Optional_inline}}</dt> - <dd>Chỉ mục của phần tử đang được kiểm tra.</dd> - <dt><code>array</code>{{Optional_inline}}</dt> - <dd>Là bản thân mảng đã gọi phương thức <code>some()</code> ở trên.</dd> - </dl> - </dd> - <dt><code>thisArg</code>{{Optional_inline}}</dt> - <dd>Được sử dụng làm giá trị <code>this</code> khi thực thi hàm <code>callback</code>.</dd> -</dl> - -<h3 id="Giá_trị_trả_về">Giá trị trả về</h3> - -<p><code><strong>true</strong></code> khi hàm <code>callback</code> trả về một giá trị {{Glossary("truthy")}} nếu có ít nhất một phần tử của mảng thoả điều kiện. Ngược lại sẽ trả về <code><strong>false</strong></code>.</p> - -<h2 id="Mô_tả">Mô tả</h2> - -<p>Phương thức <code>some()</code> thực thi hàm <code>callback</code> một lần và lặp qua từng phần tử của mảng cho đến khi hàm <code>callback</code> trả về một giá trị <em>truthy</em> (tức là <strong><code>true</code></strong> khi được chuyển sang kiểu Boolean). Nếu như có một phần tử thoả mãn, <code>some()</code> sẽ lập tức trả về <code>true</code>. Ngược lại <code>sẽ trả về</code> <code>false</code>. <code>callback</code> được gọi chỉ khi các phần tử của mảng có giá trị.</p> - -<p><code>callback</code> được gọi với ba đối số: giá trị của phần tử, số chỉ mục của phần tử và mảng đang được lặp qua.</p> - -<p>Nếu như tham số <code>thisArg</code> được truyền vào <code>some()</code>, nó sẽ được sử dụng làm giá trị <code>this</code> của <code>callback</code>. Nếu bỏ qua, <code>this</code> sẽ có giá trị {{jsxref("undefined")}}. The <code>this</code> value ultimately observable by <code>callback</code> is determined according to <a href="/en-US/docs/Web/JavaScript/Reference/Operators/this">the usual rules for determining the <code>this</code> seen by a function</a>.</p> - -<p><code>some()</code> không làm thay đổi mảng ban đầu.</p> - -<p>The range of elements processed by <code>some()</code> is set before the first invocation of <code>callback</code>. Elements appended to the array after the call to <code>some()</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>some()</code> visits that element's index. Elements that are deleted are not visited.</p> - -<h2 id="Ví_dụ">Ví dụ</h2> - -<h3 id="Kiểm_tra_giá_trị_của_các_phần_tử">Kiểm tra giá trị của các phần tử</h3> - -<p>Ví dụ bên dưới đang kiểm tra xem có phần tử nào lớn hơn 10 hay không.</p> - -<pre class="brush: js">function isBiggerThan10(element, index, array) { - return element > 10; -} - -[2, 5, 8, 1, 4].some(isBiggerThan10); // false -[12, 5, 8, 1, 4].some(isBiggerThan10); // true -</pre> - -<h3 id="Kiểm_tra_giá_trị_của_các_phần_tử_sử_dụng_arrow_function">Kiểm tra giá trị của các phần tử sử dụng arrow function</h3> - -<p><a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">Arrow functions</a> làm cho cú pháp trở nên gọn hơn.</p> - -<pre class="brush: js">[2, 5, 8, 1, 4].some(x => x > 10); // false -[12, 5, 8, 1, 4].some(x => x > 10); // true -</pre> - -<h3 id="Kiểm_tra_phần_tử_có_tồn_tại_trong_mảng_hay_không">Kiểm tra phần tử có tồn tại trong mảng hay không</h3> - -<p>Hàm <code>checkAvailability()</code> bên dưới đang mô phỏng lại phương thức <code>includes()</code>, trả về <code>true</code> nếu phần tử có tồn tại trong mảng:</p> - -<pre class="brush: js">var fruits = ['apple', 'banana', 'mango', 'guava']; - -function checkAvailability(arr, val) { - return arr.some(function(arrVal) { - return val === arrVal; - }); -} - -checkAvailability(fruits, 'kela'); // false -checkAvailability(fruits, 'banana'); // true</pre> - -<h3 id="Kiểm_tra_phần_tử_có_tồn_tại_trong_mảng_hay_không_sử_dụng_arrow_function">Kiểm tra phần tử có tồn tại trong mảng hay không sử dụng arrow function</h3> - -<pre class="brush: js">var fruits = ['apple', 'banana', 'mango', 'guava']; - -function checkAvailability(arr, val) { - return arr.some(arrVal => val === arrVal); -} - -checkAvailability(fruits, 'kela'); // false -checkAvailability(fruits, 'banana'); // true</pre> - -<h3 id="Chuyển_giá_trị_bất_kì_sang_kiểu_Boolean">Chuyển giá trị bất kì sang kiểu Boolean</h3> - -<pre class="brush: js">var TRUTHY_VALUES = [true, 'true', 1]; - -function getBoolean(value) { - 'use strict'; - - if (typeof value === 'string') { - value = value.toLowerCase().trim(); - } - - return TRUTHY_VALUES.some(function(t) { - return t === value; - }); -} - -getBoolean(false); // false -getBoolean('false'); // false -getBoolean(1); // true -getBoolean('true'); // true</pre> - -<h2 id="Polyfill">Polyfill</h2> - -<p><code>some()</code> was added to the ECMA-262 standard in the 5th edition, and it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of <code>some()</code> in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming {{jsxref("Object")}} and {{jsxref("TypeError")}} have their original values and that <code>fun.call</code> evaluates to the original value of {{jsxref("Function.prototype.call()")}}.</p> - -<pre class="brush: js">// Production steps of ECMA-262, Edition 5, 15.4.4.17 -// Reference: http://es5.github.io/#x15.4.4.17 -if (!Array.prototype.some) { - Array.prototype.some = function(fun, thisArg) { - 'use strict'; - - if (this == null) { - throw new TypeError('Array.prototype.some called on null or undefined'); - } - - if (typeof fun !== 'function') { - throw new TypeError(); - } - - var t = Object(this); - var len = t.length >>> 0; - - for (var i = 0; i < len; i++) { - if (i in t && fun.call(thisArg, t[i], i, t)) { - return true; - } - } - - return false; - }; -} -</pre> - -<h2 id="Đặc_tả">Đặc tả</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Đặc tả</th> - <th scope="col">Trạng thái</th> - <th scope="col">Chú thích</th> - </tr> - <tr> - <td>{{SpecName('ES5.1', '#sec-15.4.4.17', 'Array.prototype.some')}}</td> - <td>{{Spec2('ES5.1')}}</td> - <td>Được đưa vào lần đầu trong JavaScript 1.6.</td> - </tr> - <tr> - <td>{{SpecName('ES6', '#sec-array.prototype.some', 'Array.prototype.some')}}</td> - <td>{{Spec2('ES6')}}</td> - <td></td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-array.prototype.some', 'Array.prototype.some')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td></td> - </tr> - </tbody> -</table> - -<h2 id="Khả_năng_tương_thích_của_trình_duyệt">Khả năng tương thích của trình duyệt</h2> - -<div> - - -<p>{{Compat("javascript.builtins.Array.some")}}</p> -</div> - -<h2 id="Xem_thêm">Xem thêm</h2> - -<ul> - <li>{{jsxref("Array.prototype.forEach()")}}</li> - <li>{{jsxref("Array.prototype.every()")}}</li> - <li>{{jsxref("Array.prototype.find()")}}</li> - <li>{{jsxref("TypedArray.prototype.some()")}}</li> -</ul> |
