The secret to wholesale...

In the article about buying Thai goods, I introduced wholesale markets and cheap...

Restaurant business and the...

Customers are a special figure in restaurants. This may seem obvious, but...

Guide the clues to...

Japanese goods Known for putting quality first. However, in the past, due...

Online cosmetics business and...

Starting a business, especially online cosmetics business is the silver trend and...
HomeLập trìnhJavaScriptGiải thích về...

Giải thích về độ dài mảng JavaScript


length là một thuộc tính của mảng trong JavaScript trả về hoặc đặt số lượng phần tử trong một mảng nhất định.

Các length thuộc tính của một mảng có thể được trả lại như vậy.

let desserts = ["Cake", "Pie", "Brownies"];
console.log(desserts.length); // 3

Toán tử gán, kết hợp với length thuộc tính, có thể được sử dụng để đặt số lượng phần tử trong một mảng như vậy.

let cars = ["Saab", "BMW", "Volvo"];
cars.length = 2;
console.log(cars.length); // 2

Thông tin thêm về mảng:

phương thức isArray()

Các Array.isArray() phương thức trả về true nếu một đối tượng là một mảng, false nếu không phải vậy.

Cú pháp:

Array.isArray(obj)

Thông số:

đối tượng Đối tượng cần kiểm tra.

liên kết MDN | liên kết MSDN

Ví dụ:

// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// Little known fact: Array.prototype itself is an array:
Array.isArray(Array.prototype); 

// 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({ __proto__: Array.prototype });

Array.prototype.forEach

Phương thức mảng ‘forEach’ được sử dụng để lặp qua từng mục trong một mảng. Phương thức này được gọi trên đối tượng mảng và được truyền một hàm được gọi trên mỗi mục trong mảng.

var arr = [1, 2, 3, 4, 5];

arr.forEach(number => console.log(number * 2));

// 2
// 4
// 6
// 8
// 10

Hàm gọi lại cũng có thể lấy tham số thứ hai của chỉ mục trong trường hợp bạn cần tham chiếu chỉ mục của mục hiện tại trong mảng.

var arr = [1, 2, 3, 4, 5];

arr.forEach((number, i) => console.log(`${number} is at index ${i}`));

// '1 is at index 0'
// '2 is at index 1'
// '3 is at index 2'
// '4 is at index 3'
// '5 is at index 4'

Đọc thêm về mảng:

mảng.prototype.filter

Đọc thêm  JavaScript chuyển đổi chuỗi thành số - Ví dụ về chuỗi JS thành Int

mảng.prototype.reduce



Zik.vn – Biên dịch & Biên soạn Lại

spot_img

Create a website from scratch

Just drag and drop elements in a page to get started with Newspaper Theme.

Buy Now ⟶

Bài viết liên quang

DMCA.com Protection Status