Trong bài này chúng ta sẽ tìm hiểu hàm join trong javascript, đây là hàm dùng để nối các phần tử của mảng lại với nhau thành một chuỗi.
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.
Hàm join sẽ nối các phần tử của mảng thành một chuỗi, các phần tử được ngăn cách nhau bởi kí tự do người dùng quy định. Nếu không truyền ký tự ngăn cách vào thì giá trị mặc định là dấu phẩy ",".
Nếu bạn chỉ đơn thuần muốn chuyển mảng thành chuỗi và ngăn cách bởi dấu phẩy thì hãy sử dụng hàm array.toString() nhé.
1. Cú pháp hàm join trong javascript
Hàm join có cú pháp như sau:
Bài viết này được đăng tại [free tuts .net]
Trong đó:
- separator là kí tự sẽ ngăn cách các phần tử với nhau, mặc định mang giá trị là dấu ",".
- Hàm sẽ trả về một chuỗi, và mảng cũ không ảnh hưởng gì cả.
Hàm này rất ít khi sử dụng, bởi thao tác chuyển mảng thành chuỗi trong các ứng dụng thực tế rất hiếm gặp.
Ví dụ: Đây là một demo mình lấy từ trang chủ của mozilla.
var a = ['Wind', 'Water', 'Fire']; a.join(); // 'Wind,Water,Fire' a.join(', '); // 'Wind, Water, Fire' a.join(' + '); // 'Wind + Water + Fire' a.join(''); // 'WindWaterFire'
2. Một ví dụ khác về hàm join trong javascript
Hãy viết chương trình khi click vào một button thì chuyển tất cả các phần tử của mảng subject thành một chuỗi, sau đó in lên màn hình.
<h2>Học lập trình miễn phí tại freetuts.net</h2> <button title="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var subject = ["PHP", "HTML", "CSS", "JS"]; var x = document.getElementById("demo"); x.innerHTML = subject.join(); } </script>
3. So sánh hàm join với array.toString
Cả hai hàm đều là các phương thức của đối tượng mảng, và công dụng la chuyển đổi mảng thành chuỗi. Tuy nhiên, chúng sẽ có một chút khác biệt như sau:
- Hàm join có thể tùy biến ký tự ngăn cách giữa các phần tử.
- Hàm array.toString thì luôn sử dụng dấu phẩy để ngăn cách giữa các phần tử.
Trên là tất cả những thông tin cần biết về hàm join trong js.
The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator. separator Optional Specifies a string to separate each pair of adjacent elements of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma (","). If separator is an empty string, all
elements are joined without any characters in between them. A string with all array elements joined. If arr.length is 0, the empty string is returned. The string conversions of all array elements are joined into one string. Warning: If an element is undefined, null or an empty array [], it is converted to an empty string. The join method is accessed internally by
Array.prototype.toString() with no arguments. Overriding join of an array instance will override its toString behavior as well. The following example creates an array, a, with three elements, then joins the array four times: using the default
separator, then a comma and a space, then a plus and an empty string. const a = ['Wind', 'Water', 'Fire'];
a.join(); // 'Wind,Water,Fire'
a.join(', '); // 'Wind, Water, Fire'
a.join(' + '); // 'Wind + Water + Fire'
a.join(''); // 'WindWaterFire'
The following example joins array-like object
(arguments), by calling Function.prototype.call on Array.prototype.join. function f(a, b, c) {
const s = Array.prototype.join.call(arguments);
console.log(s); // '1,a,true'
}
f(1, 'a', true);
//expected output: "1,a,true"
BCD tables only load in the browserTry it
Syntax
Parameters
Return value
Description
Examples
Joining an array four different ways
Joining an array-like object
Specifications
Specification ECMAScript Language Specification
# sec-array.prototype.join
Browser compatibility
See also