Bài 7: Array trong Javascript (Phần 1)
Cũng giống như đa phần các ngôn ngữ lập trình khác, trong Javascript, Array là một đối tượng cho phép lưu trữ một tập hợp các phần tử dưới dạng một biến duy nhất và cho phép thực thi các tính toán đối với cả tập hợp cũng như các đối tượng bên trong.
Đặc điểm:
- Mảng JavaScript có thể thay đổi kích thước và có thể chứa nhiều kiểu dữ liệu khác nhau
- Các phần tử trong mảng có thể truy cập dựa vào chỉ mục của nó
- Chỉ mục được tính từ 0, nghĩa là phần tử đầu tiên trong mảng bắt đầu với chỉ mục là 0
Nội dung của bài
Khởi tạo một Array
Có nhiều cách để khởi tạo một Array trong Javascript, bạn có thể tham khảo các đoạn mã bên dưới, khuyến khích bạn khởi tạo mảng theo cách đầu tiên:
// better than using the Array() constructor
const array = [];
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log(fruits.length);
// 2
// 'fruits' array created using the Array() constructor.
const fruits = new Array('Apple', 'Banana');
console.log(fruits.length);
// 2
// 'fruits' array created using String.prototype.split().
const fruits = 'Apple, Banana'.split(', ');
console.log(fruits.length);
// 2
Lấy độ dài mảng
Thuộc length
tính của một đối tượng là một thuộc tính của Array, nó trả về số phần tử trong mảng đó.
const texts = ['welcome', 'to', 'Yeulaptrinh', '!'];
console.log(texts.length);
// expected output: 4
Truy cập tới một phần tử trong mảng
Ta sử dụng index của phần tử đó để truy cập. Lưu ý rằng index bắt đầu từ 0.
const texts = ['welcome', 'to', 'Yeulaptrinh', '!'];
console.log(texts[0]);
// first element: 'welcome'
console.log(texts[2]);
// 'Yeulaptrinh'
console.log(texts[texts.length-1]);
// '!'
Kiểm tra đối tượng có phải là một Array?
Javascript cung cấp method Array.isArray()
để kiểm tra xem một đối tượng có phải là Array hay không.
// Syntax
Array.isArray(value);
// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
Array.isArray(new Array('a', 'b', 'c', 'd'));
Array.isArray(new Array(3));
// all following calls return false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray('Array');
Array.isArray(true);
Array.isArray(false);
Array.isArray(new Uint8Array(32));
Hợp nhất 2 hay nhiều mảng
Phương thức concat ()
được sử dụng để hợp nhất hai hoặc nhiều mảng. Phương thức này không thay đổi các mảng hiện có mà thay vào đó trả về một mảng mới.
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]
/* Concatenating three arrays */
const num1 = [1, 2, 3];
const num2 = [4, 5, 6];
const num3 = [7, 8, 9];
const numbers = num1.concat(num2, num3);
console.log(numbers);
// results in [1, 2, 3, 4, 5, 6, 7, 8, 9];
/* Concatenating nested arrays */
const num1 = [[1]];
const num2 = [2, [3]];
const numbers = num1.concat(num2);
console.log(numbers);
// results in [[1], 2, [3]]
Tổng kết
Trên đây là những khái niệm cơ bản cũng như một vài hàm thường dùng trong Javascript. Ta sẽ tìm hiểu thêm các hàm khác trong bài viết tiếp theo.