Nếu bạn đang sử dụng ES2015 và bạn muốn xác định đối tượng của riêng mình lặp lại như mảng 2 chiều, bạn có thể triển khai giao thức Iterator bằng cách:
- Xác định hàm @@ iterator gọi là
4 trả về ...var myArray = [ [1,1,1,1,1], [1,1,1,1,1], [1,1,1,1,1] ]; //don't forget your semi-colons
- ... một đối tượng có chức năng
5 trả về ...var myArray = [ [1,1,1,1,1], [1,1,1,1,1], [1,1,1,1,1] ]; //don't forget your semi-colons
- ... Một đối tượng có một hoặc hai thuộc tính: một
6 tùy chọn với giá trị tiếp theo [nếu có một] và booleanvar myArray = [ [1,1,1,1,1], [1,1,1,1,1], [1,1,1,1,1] ]; //don't forget your semi-colons
7 là đúng nếu chúng ta thực hiện lặp lại.var myArray = [ [1,1,1,1,1], [1,1,1,1,1], [1,1,1,1,1] ]; //don't forget your semi-colons
Chức năng lặp mảng một chiều sẽ trông như thế này:
// our custom Cubes object which implements the iterable protocol
function Cubes[] {
this.cubes = [1, 2, 3, 4];
this.numVals = this.cubes.length;
// assign a function to the property Symbol.iterator
// which is a special property that the spread operator
// and for..of construct both search for
this[Symbol.iterator] = function [] { // can't take args
var index = -1; // keep an internal count of our index
var self = this; // access vars/methods in object scope
// the @@iterator method must return an object
// with a "next[]" property, which will be called
// implicitly to get the next value
return {
// next[] must return an object with a "done"
// [and optionally also a "value"] property
next: function[] {
index++;
// if there's still some values, return next one
if [index < self.numVals] {
return {
value: self.cubes[index],
done: false
};
}
// else there's no more values left, so we're done
// IF YOU FORGET THIS YOU WILL LOOP FOREVER!
return {done: true}
}
};
};
}
Bây giờ, chúng ta có thể coi đối tượng
var myArray = [
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]
]; //don't forget your semi-colons
8 của chúng ta như một điều không thể sử dụng được:var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
Để tạo ra 2-D có thể lặp lại, thay vì trả về một giá trị trong hàm
var myArray = [
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]
]; //don't forget your semi-colons
5 của chúng tôi, chúng tôi có thể trả về một điều khác có thể sử dụng được:function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
Bây giờ, chúng ta có thể sử dụng lặp lại lồng nhau:
var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
Lưu ý rằng có thể điều chỉnh tùy chỉnh của chúng tôi sẽ không hoạt động như một mảng 2 chiều trong mọi trường hợp; Ví dụ: chúng tôi chưa thực hiện chức năng
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
0. Câu trả lời này cho thấy cách bạn có thể triển khai chức năng bản đồ máy phát [xem ở đây để biết sự khác biệt giữa trình lặp và máy phát điện; Ngoài ra, trình tạo là một tính năng ES2016, không phải ES2015, vì vậy bạn sẽ cần thay đổi cài đặt trước Babel nếu bạn biên dịch với Babel ]. Lặp lại trên hai chiều có nghĩa là bạn sẽ cần kiểm tra hai chiều.
Nội phân chính
- Sự mô tả
- Người xây dựng
- Tính chất tĩnh
- Phương pháp tĩnh
- Thuộc tính thể hiện
- Phương pháp thể hiện
- Tạo một mảng
- Tạo một chuỗi từ một mảng
- Truy cập một mục mảng theo chỉ mục của nó
- Tìm chỉ mục của một mục trong một mảng
- Kiểm tra xem một mảng có chứa một mục nào đó không
- Nối một mục vào một mảng
- Xóa mục cuối cùng khỏi một mảng
- Xóa nhiều mục từ cuối một mảng
- Cắt ngắn một mảng xuống chỉ là n mục đầu tiên của nó
- Xóa mục đầu tiên khỏi một mảng
- Xóa nhiều mục từ đầu một mảng
- Thêm một mục đầu tiên mới vào một mảng
- Xóa một mục duy nhất bằng chỉ mục
- Xóa nhiều mục bằng chỉ mục
- Thay thế nhiều mục trong một mảng
- Lặp lại trên một mảng
- Gọi một hàm trên mỗi phần tử trong một mảng
- Hợp nhất nhiều mảng với nhau
- Sao chép một mảng
- Nhóm các yếu tố của một mảng
- Những ví dụ khác
- Tạo một mảng hai chiều
- Sử dụng một mảng để lập bảng một tập hợp các giá trị
- Tạo một mảng bằng kết quả của một trận đấu
- Mối quan hệ giữa độ dài và tính chất số
- Phương pháp mảng và các khe trống
- Sao chép phương pháp và phương pháp đột biến
- Phương pháp mảng chung
- Thông số kỹ thuật
- Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt
Xem thêmLặp lại trên hai chiều có nghĩa là bạn sẽ cần kiểm tra hai chiều.
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
Nội phân chính
Sự mô tả
var myArray = [
[...],
[...],
[...]
];
Người xây dựng
var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
Tính chất tĩnh
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
Sự mô tả
Người xây dựng
- Tính chất tĩnh and can contain a mix of different data types. [When those characteristics are undesirable, use typed arrays instead.]
- Phương pháp tĩnh and so, array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using nonnegative integers [or their respective string form] as indexes.
- Thuộc tính thể hiện: the first element of an array is at index
2, the second is at indexvar myArray = [ [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], ];
3, and so on — and the last element is at the value of the array'svar myArray = [ [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], ];
4 property minusvar myArray = [ [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], ];
3.var myArray = [ [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], ];
- Phương pháp thể hiện. [All standard built-in copy operations with any JavaScript objects create shallow copies, rather than deep copies].
Người xây dựng
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
6Tính chất tĩnh
Tính chất tĩnh
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
8Phương pháp tĩnh
Phương pháp tĩnh
var myArray = [
[...],
[...],
[...]
];
0Thuộc tính thể hiện
var myArray = [
[...],
[...],
[...]
];
2Phương pháp thể hiện
var myArray = [
[...],
[...],
[...]
];
5Tạo một mảng
Thuộc tính thể hiện
var myArray = [
[...],
[...],
[...]
];
7Phương pháp thể hiện
var myArray = [
[...],
[...],
[...]
];
8Tạo một mảng
Phương pháp thể hiện
Tạo một mảng
Tạo một chuỗi từ một mảng
var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
1Truy cập một mục mảng theo chỉ mục của nó
var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
2Tìm chỉ mục của một mục trong một mảng
var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
3Kiểm tra xem một mảng có chứa một mục nào đó không
var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
4Trả về
var myArray = [
[...],
[...],
[...]
];
3 Nếu mọi phần tử trong mảng gọi thỏa mãn chức năng kiểm tra.var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
6Điền vào tất cả các phần tử của một mảng từ chỉ mục bắt đầu sang chỉ mục cuối với giá trị tĩnh.
var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
7Trả về một mảng mới chứa tất cả các phần tử của mảng gọi mà hàm lọc được cung cấp trả về
var myArray = [
[...],
[...],
[...]
];
3.var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
9Trả về giá trị của phần tử đầu tiên trong mảng thỏa mãn chức năng thử nghiệm được cung cấp hoặc
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
0 nếu không tìm thấy phần tử thích hợp.var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
1Trả về chỉ số của phần tử đầu tiên trong mảng thỏa mãn chức năng thử nghiệm được cung cấp hoặc
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
2 nếu không tìm thấy phần tử thích hợp.var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
3Trả về giá trị của phần tử cuối cùng trong mảng thỏa mãn chức năng thử nghiệm được cung cấp hoặc
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
0 nếu không tìm thấy phần tử thích hợp.var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
5Trả về chỉ số của phần tử cuối cùng trong mảng thỏa mãn chức năng thử nghiệm được cung cấp hoặc
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
2 nếu không tìm thấy phần tử thích hợp.var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
7Trả về một mảng mới với tất cả các yếu tố mảng con được liên kết vào nó đệ quy lên đến độ sâu được chỉ định.
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
8Trả về một mảng mới được hình thành bằng cách áp dụng một hàm gọi lại đã cho cho từng phần tử của mảng gọi, sau đó làm phẳng kết quả theo một cấp.
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
9Gọi một hàm cho mỗi phần tử trong mảng gọi.
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
0 Thử nghiệmExperimentalNhóm các phần tử của một mảng thành một đối tượng theo các chuỗi được trả về bởi một hàm thử nghiệm.
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
1 Thử nghiệmExperimentalNhóm các phần tử của một mảng thành
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
2 theo các giá trị được trả về bởi hàm thử nghiệm.// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
3Xác định xem mảng gọi có chứa giá trị hay không, trả về
var myArray = [
[...],
[...],
[...]
];
3 hoặc var myArray = [
[...],
[...],
[...]
];
4 nếu thích hợp.// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
6Trả về chỉ mục đầu tiên [ít nhất] mà tại đó một phần tử nhất định có thể được tìm thấy trong mảng gọi.
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
7Tham gia tất cả các yếu tố của một mảng thành một chuỗi.
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
8Trả về một trình lặp mảng mới chứa các phím cho mỗi chỉ mục trong mảng gọi.
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
9Trả về chỉ mục cuối cùng [lớn nhất] mà tại đó một phần tử nhất định có thể được tìm thấy trong mảng gọi hoặc
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
2 nếu không tìm thấy.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
01Trả về một mảng mới chứa kết quả gọi một hàm trên mọi phần tử trong mảng gọi.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
02Loại bỏ phần tử cuối cùng khỏi một mảng và trả về phần tử đó.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
03Thêm một hoặc nhiều phần tử vào cuối một mảng và trả về
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4 mới của mảng.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
05Thực thi chức năng gọi lại "giảm" do người dùng cung cấp trên mỗi phần tử của mảng [từ trái sang phải], để giảm nó xuống một giá trị duy nhất.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
06Thực thi chức năng gọi lại "giảm" do người dùng cung cấp trên mỗi phần tử của mảng [từ phải sang trái], để giảm nó xuống một giá trị duy nhất.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
07Đảo ngược thứ tự của các yếu tố của một mảng tại chỗ. [Đầu tiên trở thành người cuối cùng, lần cuối cùng trở thành đầu tiên.]
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
08Loại bỏ phần tử đầu tiên khỏi một mảng và trả về phần tử đó.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
09Trích xuất một phần của mảng gọi và trả về một mảng mới.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
10Trả về
var myArray = [
[...],
[...],
[...]
];
3 Nếu ít nhất một phần tử trong mảng gọi thỏa mãn chức năng kiểm tra được cung cấp.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
12Sắp xếp các phần tử của một mảng tại chỗ và trả về mảng.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
13Thêm và/hoặc loại bỏ các yếu tố khỏi một mảng.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
14Trả về một chuỗi cục bộ đại diện cho mảng gọi và các yếu tố của nó. Ghi đè phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
15.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
16Trả về một chuỗi đại diện cho mảng gọi và các phần tử của nó. Ghi đè phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
17.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
18Thêm một hoặc nhiều phần tử vào mặt trước của một mảng và trả về
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4 mới của mảng.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
20Trả về một đối tượng lặp mảng mới chứa các giá trị cho mỗi chỉ mục trong mảng.
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
21Một bí danh cho phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
22 theo mặc định.Ví dụ
Phần này cung cấp một số ví dụ về các hoạt động mảng phổ biến trong JavaScript.
Tạo một mảng
Ví dụ này hiển thị ba cách để tạo mảng mới: đầu tiên sử dụng ký hiệu theo nghĩa đen của mảng, sau đó sử dụng hàm tạo
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
6 và cuối cùng sử dụng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
24 để xây dựng mảng từ một chuỗi.// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
Tạo một chuỗi từ một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
25 để tạo một chuỗi từ mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
0Truy cập một mục mảng theo chỉ mục của nó
Ví dụ này cho thấy cách truy cập các mục trong mảng
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 bằng cách chỉ định số chỉ mục của vị trí của chúng trong mảng.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
1Tìm chỉ mục của một mục trong một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
28 để tìm vị trí [chỉ mục] của chuỗi var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
29 trong mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
2Kiểm tra xem một mảng có chứa một mục nào đó không
Ví dụ này cho thấy hai cách để kiểm tra xem mảng
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 có chứa var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
29 và var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
33: Đầu tiên với phương thức var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
34, sau đó với phương thức var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
28 để kiểm tra giá trị chỉ mục không phải là var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
2.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
3Nối một mục vào một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
37 để nối một chuỗi mới vào mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
4Xóa mục cuối cùng khỏi một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
39 để xóa mục cuối cùng khỏi mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
5Lưu ý:
39 chỉ có thể được sử dụng để loại bỏ mục cuối cùng khỏi một mảng. Để loại bỏ nhiều mục từ cuối một mảng, hãy xem ví dụ tiếp theo. var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
39 can only be used to
remove the last item from an array. To remove multiple items from the end of an array, see the next example.Xóa nhiều mục từ cuối một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
42 để loại bỏ 3 mục cuối cùng khỏi mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
6Cắt ngắn một mảng xuống chỉ là n mục đầu tiên của nó
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
42 để cắt giảm mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 xuống chỉ còn 2 mục đầu tiên.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
7Xóa mục đầu tiên khỏi một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
46 để xóa mục đầu tiên khỏi mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
8Lưu ý:
46 chỉ có thể được sử dụng để loại bỏ mục đầu tiên khỏi một mảng. Để loại bỏ nhiều mục từ đầu một mảng, hãy xem ví dụ tiếp theo. var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
46 can only be used to remove the first item from an array. To remove multiple items from the beginning of an array, see the next example.Xóa nhiều mục từ đầu một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
42 để loại bỏ 3 mục đầu tiên khỏi mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26.var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
9Thêm một mục đầu tiên mới vào một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
51 để thêm, tại Index var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
2, một mục mới cho mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 - biến nó thành mục đầu tiên mới trong mảng.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
0Xóa một mục duy nhất bằng chỉ mục
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
42 để xóa chuỗi var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
29 khỏi mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 - bằng cách chỉ định vị trí chỉ mục của var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
29.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
1Xóa nhiều mục bằng chỉ mục
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
42 để loại bỏ các chuỗi var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
29 và var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
60 khỏi mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 - bằng cách chỉ định vị trí chỉ mục của var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
29, cùng với số lượng tổng số mục để xóa.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
2Thay thế nhiều mục trong một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
42 để thay thế 2 mục cuối cùng trong mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 bằng các mục mới.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
3Lặp lại trên một mảng
Ví dụ này sử dụng vòng lặp
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
65 để lặp qua mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26, đăng nhập từng mục vào bảng điều khiển.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
4Nhưng
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
65 chỉ là một trong nhiều cách để lặp lại bất kỳ mảng nào; Để biết thêm các cách, xem các vòng lặp và lặp lại, và xem tài liệu cho var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
68, var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
69, var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
70, var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
0, var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
72 và var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
73 - và xem ví dụ tiếp theo, sử dụng phương thức var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
74.Gọi một hàm trên mỗi phần tử trong một mảng
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
74 để gọi hàm trên mỗi phần tử trong mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26; Hàm làm cho mỗi mục được ghi vào bảng điều khiển, cùng với số chỉ mục của mục.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
5Hợp nhất nhiều mảng với nhau
Ví dụ này sử dụng phương thức
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
77 để hợp nhất mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 với mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
79, để tạo ra một mảng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
80 mới. Lưu ý rằng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 và var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
79 vẫn không thay đổi.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
6Sao chép một mảng
Ví dụ này cho thấy ba cách để tạo một mảng mới từ mảng
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
26 hiện có: đầu tiên bằng cách sử dụng cú pháp lan truyền, sau đó bằng cách sử dụng phương thức var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
84, sau đó bằng cách sử dụng phương thức var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
85.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
7Tất cả các hoạt động sao chép mảng tích hợp [Cú pháp lan truyền,
var myArray = [
[...],
[...],
[...]
];
0, var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
09 và var i,
rows,
myArray;
rows = 8;
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray.push[[]];
}
}
1] tạo các bản sao nông. Thay vào đó, nếu bạn muốn có một bản sao sâu của một mảng, bạn có thể sử dụng var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
89 để chuyển đổi mảng thành chuỗi JSON và sau đó var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
90 để chuyển đổi chuỗi trở lại thành một mảng mới hoàn toàn độc lập với mảng gốc.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
8Bạn cũng có thể tạo các bản sao sâu bằng phương pháp
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
91, có lợi thế cho phép các đối tượng có thể chuyển được trong nguồn được chuyển sang bản sao mới, thay vì chỉ nhân bản.Cuối cùng, điều quan trọng là phải hiểu rằng việc gán một mảng hiện có cho một biến mới không tạo ra một bản sao của mảng hoặc các phần tử của nó. Thay vào đó, biến mới chỉ là một tham chiếu, hoặc bí danh, cho mảng gốc; Đó là, tên của mảng gốc và tên biến mới chỉ là hai tên cho cùng một đối tượng [và do đó sẽ luôn đánh giá là tương đương nghiêm ngặt]. Do đó, nếu bạn thực hiện bất kỳ thay đổi nào theo giá trị của mảng gốc hoặc với giá trị của biến mới, thì cũng sẽ thay đổi:
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
9Nhóm các yếu tố của một mảng
Các phương thức
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
0 có thể được sử dụng để nhóm các phần tử của một mảng, sử dụng hàm thử nghiệm trả về một chuỗi biểu thị nhóm của phần tử hiện tại.Ở đây chúng tôi có một mảng hàng tồn kho đơn giản có chứa các đối tượng "thực phẩm" có
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
93 và var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
94.var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
0Để sử dụng
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
95, bạn cung cấp chức năng gọi lại được gọi với phần tử hiện tại và tùy chọn chỉ mục và mảng hiện tại và trả về một chuỗi chỉ ra nhóm của phần tử.Mã bên dưới sử dụng hàm mũi tên để trả về
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
94 của mỗi phần tử mảng [điều này sử dụng cú pháp phá hủy đối tượng cho các đối số chức năng để giải nén phần tử var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
94 khỏi đối tượng được truyền]. Kết quả là một đối tượng có các thuộc tính được đặt tên theo các chuỗi duy nhất được trả về bởi cuộc gọi lại. Mỗi thuộc tính được gán một mảng chứa các phần tử trong nhóm.var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
1Lưu ý rằng đối tượng được trả về tham chiếu các phần tử giống như mảng gốc [không phải bản sao sâu]. Thay đổi cấu trúc bên trong của các yếu tố này sẽ được phản ánh trong cả mảng gốc và đối tượng được trả về.
Ví dụ: nếu bạn không thể sử dụng chuỗi làm khóa làm khóa, nếu thông tin đến nhóm được liên kết với một đối tượng có thể thay đổi, thì thay vào đó bạn có thể sử dụng
// 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
1. Điều này rất giống với var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
99 ngoại trừ việc nó nhóm các phần tử của mảng thành // 'fruits' array created using array literal notation.
const fruits = ['Apple', 'Banana'];
console.log[fruits.length];
// 2
// 'fruits2' array created using the Array[] constructor.
const fruits2 = new Array['Apple', 'Banana'];
console.log[fruits2.length];
// 2
// 'fruits3' array created using String.prototype.split[].
const fruits3 = 'Apple, Banana'.split[', '];
console.log[fruits3.length];
// 2
2 có thể sử dụng giá trị tùy ý [đối tượng hoặc nguyên thủy] làm chìa khóa.Những ví dụ khác
Tạo một mảng hai chiều
Sau đây tạo ra một bàn cờ như một chuỗi hai chiều. Động thái đầu tiên được thực hiện bằng cách sao chép
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
01 trong function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
02 sang function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
03. Vị trí cũ tại function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
04 được làm trống.var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
2Đây là đầu ra:
var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
3Sử dụng một mảng để lập bảng một tập hợp các giá trị
var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
4Kết quả trong
var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
5Tạo một mảng bằng kết quả của một trận đấu
Kết quả của một trận đấu giữa
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
05 và một chuỗi có thể tạo ra một mảng JavaScript có các thuộc tính và phần tử cung cấp thông tin về trận đấu. Một mảng như vậy được trả lại bởi function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
06 và function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
07.Ví dụ:
var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
6Để biết thêm thông tin về kết quả của một trận đấu, hãy xem trang
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
06 và function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
07.Ghi chú
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
1 Các đối tượng không thể sử dụng các chuỗi tùy ý làm chỉ số phần tử [như trong một mảng kết hợp] nhưng phải sử dụng các số nguyên không âm [hoặc dạng chuỗi tương ứng của chúng]. Cài đặt hoặc truy cập thông qua những người không phải là thông tin sẽ không đặt hoặc truy xuất một phần tử từ chính danh sách mảng, nhưng sẽ đặt hoặc truy cập một biến được liên kết với bộ sưu tập thuộc tính đối tượng của mảng đó. Các thuộc tính đối tượng của mảng và danh sách các phần tử mảng là riêng biệt và các hoạt động di chuyển và đột biến của mảng không thể được áp dụng cho các thuộc tính được đặt tên này.Các phần tử mảng là các thuộc tính đối tượng giống như cách
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
11 là một thuộc tính [tuy nhiên, cụ thể, function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
12 là một phương thức]. Tuy nhiên, cố gắng truy cập một phần tử của một mảng như sau đã ném lỗi cú pháp vì tên thuộc tính không hợp lệ:var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
7Cú pháp JavaScript yêu cầu các thuộc tính bắt đầu bằng một chữ số được truy cập bằng ký hiệu khung thay vì ký hiệu DOT. Cũng có thể trích dẫn các chỉ số mảng [ví dụ:
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
13 thay vì function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
14], mặc dù thường không cần thiết.function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
15 trong function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
14 được ép thành một chuỗi bởi công cụ JavaScript thông qua chuyển đổi function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
11 ngầm. Do đó, function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
18 và function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
19 sẽ đề cập đến hai vị trí khác nhau trên đối tượng function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
20 và ví dụ sau đây có thể là var myArray = [
[...],
[...],
[...]
];
3:var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
8Chỉ
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
13 là một chỉ số mảng thực tế. function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
23 là một thuộc tính chuỗi tùy ý sẽ không được truy cập trong vòng lặp mảng.Mối quan hệ giữa độ dài và tính chất số
Thuộc tính
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4 của mảng JavaScript được kết nối.Một số phương thức mảng tích hợp [ví dụ:
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
25, var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
85, var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
28, v.v.] có tính đến giá trị của thuộc tính var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4 của mảng khi chúng được gọi.Các phương thức khác [ví dụ:
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
37, var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
42, v.v.] cũng dẫn đến các bản cập nhật cho thuộc tính var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4 của mảng.var cube = new Cubes[];
// spread operator returns list of iterators,
// each of which can be spread to get values
var rows = [...cube];
console.log[[...rows[0]]];
console.log[[...rows[1]]];
console.log[[...rows[2]]];
// use map to apply spread operator to each iterable
console.log[[...cube].map[function[iterator] {
return [...iterator];
}]];
for [var row of cube] {
for [var value of row] {
console.log[value];
}
}
9Khi đặt thuộc tính trên mảng JavaScript khi thuộc tính là chỉ mục mảng hợp lệ và chỉ mục đó nằm ngoài giới hạn hiện tại của mảng, động cơ sẽ cập nhật thuộc tính
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4 của mảng phù hợp:var myArray = [
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]
]; //don't forget your semi-colons
0Tăng
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4. var myArray = [
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]
]; //don't forget your semi-colons
1Giảm thuộc tính
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4, tuy nhiên, xóa các yếu tố.var myArray = [
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]
]; //don't forget your semi-colons
2Điều này được giải thích thêm trên trang
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
35.Phương pháp mảng và các khe trống
Các khe trống trong các mảng thưa thớt cư xử không nhất quán giữa các phương thức mảng. Nói chung, các phương pháp cũ hơn sẽ bỏ qua các khe trống, trong khi các phương pháp mới hơn coi chúng là
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
0.Trong số các phương pháp lặp qua nhiều yếu tố, sau đây thực hiện kiểm tra
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
37 trước khi truy cập chỉ mục và không kết hợp các khe trống với var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
0:
77var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
40function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
68var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
69var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
43function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
70var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
74var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
28var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
47function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
0var myArray = [ [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], ];
72var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
73var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
51function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
85var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
53function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
54function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
42var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
Để biết chính xác cách họ xử lý các khe trống, hãy xem trang cho mỗi phương thức.
Các phương pháp này xử lý các khe trống như thể chúng là
var i,
j,
row,
rows,
cols,
myArray;
rows = 8;
cols = 7; //adding columns in this time
myArray = [...]; //see first example above
for [i = 0; i < rows; i += 1] {
//check if the index exists in the outer array [row]
if [![i in myArray]] {
//if it doesn't exist, we need another array to fill
myArray[i] = [];
}
row = myArray[i];
for [j = 0; j < cols; j += 1] {
//check if the index exists in the inner array [column]
if [![i in row]] {
//if it doesn't exist, we need to fill it with `0`
row[j] = 0;
}
}
}
0:
57function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
58function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
59function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
60function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
61function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
62function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
95var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
64function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
34var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
25var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
67function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
68function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
22var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
Sao chép phương pháp và phương pháp đột biến
Một số phương thức không làm thay đổi mảng hiện tại mà phương thức đã được gọi, mà thay vào đó lại trả về một mảng mới. Họ làm như vậy bằng cách trước tiên truy cập
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
70 để xác định hàm tạo để sử dụng cho mảng mới. Mảng mới được xây dựng sau đó được điền với các yếu tố. Bản sao luôn xảy ra nông cạn - phương thức không bao giờ sao chép bất cứ thứ gì ngoài mảng được tạo ban đầu. Các phần tử của [các] mảng gốc được sao chép vào mảng mới như sau:- Đối tượng: Tham chiếu đối tượng được sao chép vào mảng mới. Cả mảng gốc và mảng mới đều đề cập đến cùng một đối tượng. Đó là, nếu một đối tượng được tham chiếu được sửa đổi, các thay đổi được hiển thị cho cả mảng mới và ban đầu.
- Các loại nguyên thủy như chuỗi, số và booleans [không phải
71,function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
72 vàfunction Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
73 đối tượng]: Giá trị của chúng được sao chép vào mảng mới.function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
Các phương thức khác làm biến đổi mảng mà phương thức được gọi, trong trường hợp giá trị trả về của chúng khác nhau tùy thuộc vào phương thức: đôi khi một tham chiếu đến cùng một mảng, đôi khi độ dài của mảng mới.
Các phương thức sau tạo các mảng mới với
function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
74:
77var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
69var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
43function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
70var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
0var myArray = [ [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], ];
85var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
42 [để xây dựng mảng các phần tử bị loại bỏ đã được trả về]var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
Lưu ý rằng
var cube = new Cubes[]; // construct our cube object
// both call Symbol.iterator function implicitly:
console.log[[...cube]]; // spread operator
for [var value of cube] { // for..of construct
console.log[value];
}
95 và function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
64 không sử dụng function Cubes[] {
this.cubes = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
];
this.numRows = this.cubes.length;
this.numCols = this.cubes[0].length; // assumes all rows have same length
this[Symbol.iterator] = function [] {
var row = -1;
var self = this;
// create a closure that returns an iterator
// on the captured row index
function createColIterator[currentRow] {
var col = -1;
var colIterator = {}
// column iterator implements iterable protocol
colIterator[Symbol.iterator] = function[] {
return {next: function[] {
col++;
if [col < self.numCols] {
// return raw value
return {
value: self.cubes[currentRow][col],
done: false
};
}
return {done: true};
}};
}
return colIterator;
}
return {next: function[] {
row++;
if [row < self.numRows] {
// instead of a value, return another iterator
return {
value: createColIterator[row],
done: false
};
}
return {done: true}
}};
};
}
74 để tạo các mảng mới cho mỗi mục nhập nhóm, nhưng luôn sử dụng hàm tạo var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
1 đơn giản. Về mặt khái niệm, họ cũng không sao chép các phương pháp.Các phương pháp sau đây làm biến đổi mảng ban đầu:
40function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
58function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
39var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
37var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
51function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
46var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
54function Cubes[] { this.cubes = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; this.numRows = this.cubes.length; this.numCols = this.cubes[0].length; // assumes all rows have same length this[Symbol.iterator] = function [] { var row = -1; var self = this; // create a closure that returns an iterator // on the captured row index function createColIterator[currentRow] { var col = -1; var colIterator = {} // column iterator implements iterable protocol colIterator[Symbol.iterator] = function[] { return {next: function[] { col++; if [col < self.numCols] { // return raw value return { value: self.cubes[currentRow][col], done: false }; } return {done: true}; }}; } return colIterator; } return {next: function[] { row++; if [row < self.numRows] { // instead of a value, return another iterator return { value: createColIterator[row], done: false }; } return {done: true} }}; }; }
42var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
51var cube = new Cubes[]; // construct our cube object // both call Symbol.iterator function implicitly: console.log[[...cube]]; // spread operator for [var value of cube] { // for..of construct console.log[value]; }
Phương pháp mảng chung
Các phương thức mảng luôn chung chung - chúng không truy cập bất kỳ dữ liệu nội bộ nào của đối tượng mảng. Họ chỉ truy cập các phần tử mảng thông qua thuộc tính
var myArray = [
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
];
4 và các phần tử được lập chỉ mục. Điều này có nghĩa là chúng cũng có thể được gọi trên các đối tượng giống như mảng.var myArray = [
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]
]; //don't forget your semi-colons
3Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # sec-marray-expects # sec-array-objects |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt