Hướng dẫn move item array javascript - di chuyển mảng mục javascript

Dưới đây là giải pháp ES6 Liner của tôi với tham số tùy chọn

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
9.one liner ES6 solution with an optional parameter
function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
9.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    this.splice(to, 0, ...this.splice(from, on))
  }
}

Thích ứng giải pháp đầu tiên được đề xuất bở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

Tham số

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
9 là số phần tử bắt đầu từ
// '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 bạn muốn di chuyển.

Dưới đây là một biến thể có thể chuỗi của điều này:

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]

Nếu bạn muốn tránh ô nhiễm nguyên mẫu, thì đây là chức năng độc lập:

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]

Và cuối cùng, đây là một chức năng thuần túy không làm biến đổi mảng gốc:

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}

Điều này sẽ bao gồm về cơ bản mọi biến thể được thấy trong mọi câu trả lời khác.

Sự mô tả

Trong JavaScript, các mảng không phải là nguyên thủy mà thay vào đó là các đối tượ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
3 với các đặc điểm cốt lõi sau:

  • Các mảng JavaScript có thể thay thế được và có thể chứa hỗn hợp các loại dữ liệu khác nhau. (Khi các đặc điểm đó không mong muốn, thay vào đó, hãy sử dụng các mảng được đánh máy.) and can contain a mix of different data types. (When those characteristics are undesirable, use typed arrays instead.)
  • Các mảng JavaScript không phải là mảng liên kết và do đó, các phần tử mảng không thể được truy cập bằng cách sử dụng các chuỗi tùy ý làm chỉ mục, nhưng phải được truy cập bằng các số nguyên không âm (hoặc dạng chuỗi tương ứng của chúng) làm chỉ mục. 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.
  • Các mảng JavaScript không được chỉ số bằng không: phần tử đầu tiên của một mảng là ở Index
    // '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
    
    4, phần thứ hai là tại Index
    // '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
    
    5, v.v.-và phần tử cuối cùng là giá trị của thuộc tí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
    
    6 của mảng trừ
    // '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
    
    5.
    : the first element of an array is at index
    // '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
    
    4, the second is at index
    // '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
    
    5, and so on — and the last element is at the value of the array's
    // '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
    
    6 property minus
    // '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
    
    5.
  • Các hoạt động sao chép mảng JavaScript tạo các bản sao nông. (Tất cả các hoạt động sao chép tích hợp tiêu chuẩn với bất kỳ đối tượng JavaScript nào tạo các bản sao nông, thay vì các bản sao sâu).. (All standard built-in copy operations with any JavaScript objects create shallow copies, rather than deep copies).

Người xây 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
8

Tạo một đối tượ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
3 mới.

Tính chất tĩnh

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
0

Trả về hàm tạo

// '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
3.

Phương pháp tĩnh

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
2

Tạo một thể hiện

// '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
3 mới từ một đối tượng giống như mảng hoặc đối tượng có thể lặp lại.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
4

Trả về

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
5 nếu đối số là một mảng hoặc
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
6 khác.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
7

Tạo một thể hiện

// '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
3 mới với một số lượng đối số thay đổi, bất kể số hoặc loại đối số.

Thuộc tính thể hiện

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
9

Phản ánh số lượng các yếu tố trong một mảng.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
0

Chứa các tên thuộc tính không được bao gồm trong tiêu chuẩn ECMAScript trước phiên bản ES2015 và bị bỏ qua cho các mục đích liên kết câu lệnh

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
1.

Phương pháp thể hiện

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
2

Trả về mục mảng tại chỉ mục đã cho. Chấp nhận số nguyên âm, được tính lại từ mục cuối cùng.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
3

Trả về một mảng mới là mảng gọi được nối với (các) mảng khác và/hoặc giá trị.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
4

Sao chép một chuỗi các phần tử mảng trong một mảng.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
5

Trả về một đối tượng lặp mảng mới chứa các cặp khóa/giá trị cho mỗi chỉ mục trong một mảng.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
6

Trả về

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
5 Nếu mọi phần tử trong mảng gọi thỏa mãn chức năng kiểm tra.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
8

Đ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.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
9

Trả 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ề

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
5.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
1

Trả 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

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
2 nếu không tìm thấy phần tử thích hợp.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
3

Trả 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

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
4 nếu không tìm thấy phần tử thích hợp.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
5

Trả 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

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
2 nếu không tìm thấy phần tử thích hợp.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
7

Trả 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

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
4 nếu không tìm thấy phần tử thích hợp.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
9

Trả 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.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
0

Trả 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.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
1

Gọi một hàm cho mỗi phần tử trong mảng gọi.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
2 Thử nghiệmExperimental

Nhó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.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
3 Thử nghiệmExperimental

Nhóm các phần tử của một mảng thành

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
4 theo các giá trị được trả về bởi hàm thử nghiệm.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
5

Xác định xem mảng gọi có chứa giá trị hay không, trả về

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
5 hay
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
6 nếu thích hợp.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
8

Trả 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.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
9

Tham gia tất cả các yếu tố của một mảng thành một chuỗi.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
0

Trả 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.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
1

Trả 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

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
4 nếu không tìm thấy.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
3

Trả 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.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
4

Loại bỏ phần tử cuối cùng khỏi một mảng và trả về phần tử đó.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
5

Thêm một hoặc nhiều phần tử vào cuối một mảng và trả về

// '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
6 mới của mảng.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
7

Thự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.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
8

Thự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.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3
9

Đả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.)

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
00

Loại bỏ phần tử đầu tiên khỏi một mảng và trả về phần tử đó.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
01

Trích xuất một phần của mảng gọi và trả về một mảng mới.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
02

Trả về

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
5 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.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
04

Sắp xếp các phần tử của một mảng tại chỗ và trả về mảng.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
05

Thêm và/hoặc loại bỏ các yếu tố khỏi một mảng.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
06

Trả 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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
07.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
08

Trả 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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
09.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
10

Thê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ề

// '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
6 mới của mảng.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
12

Trả 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.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
13

Một bí danh cho phương thức

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
14 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

// '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
8 và cuối cùng sử dụng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
16 để 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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
17 để tạo một chuỗi từ mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18.

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"

Truy 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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 bằng cách chỉ định số chỉ mục của vị trí của chúng trong mảng.

const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined

Tì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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
20 để tìm vị trí (chỉ mục) của chuỗi
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
21 trong mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18.

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1

Kiể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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 có chứa
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
21 và
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
25 không: đầu tiên với phương thức
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
26, sau đó với phương thức
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
20 để kiểm tra giá trị chỉ mục không phải là
const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
4.

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false

Nối một mục vào một mảng

Ví dụ này sử dụng phương thức

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
29 để nối một chuỗi mới vào mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18.

const fruits = ['Apple', 'Banana'];
const newLength = fruits.push('Orange');
console.log(fruits);
// ["Apple", "Banana", "Orange"]
console.log(newLength);
// 3

Xóa mục cuối cùng khỏi một mảng

Ví dụ này sử dụng phương thức

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
31 để xóa mục cuối cùng khỏi mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
0

Lưu ý:

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
31 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.
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
31 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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
34 để loại bỏ 3 mục cuối cùng khỏi mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
1

Cắ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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
34 để cắt giảm mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 xuống chỉ còn 2 mục đầu tiên.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
2

Xóa mục đầu tiên khỏi một mảng

Ví dụ này sử dụng phương thức

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
38 để xóa mục đầu tiên khỏi mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
3

Lưu ý:

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
38 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.
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
38 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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
34 để loại bỏ 3 mục đầu tiên khỏi mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
4

Thê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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
43 để thêm, tại Index
// '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
4, một mục mới cho mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 - biến nó thành mục đầu tiên mới trong mảng.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
5

Xóa một mục duy nhất bằng chỉ mục

Ví dụ này sử dụng phương thức

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
34 để xóa chuỗi
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
21 khỏi mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 - bằng cách chỉ định vị trí chỉ mục của
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
21.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
6

Xóa nhiều mục bằng chỉ mục

Ví dụ này sử dụng phương thức

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
34 để loại bỏ các chuỗi
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
21 và
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
52 khỏi mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 - bằng cách chỉ định vị trí chỉ mục của
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
21, cùng với số lượng tổng số mục để xóa.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
7

Thay thế nhiều mục trong một mảng

Ví dụ này sử dụng phương thức

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
34 để thay thế 2 mục cuối cùng trong mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 bằng các mục mới.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
8

Lặp lại trên một mảng

Ví dụ này sử dụng vòng lặp

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
57 để lặp qua mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18, đăng nhập từng mục vào bảng điều khiển.

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
9

Nhưng

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
57 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
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
60,
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
61,
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
62,
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
63,
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
64 và
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
65 - và xem ví dụ tiếp theo, sử dụng phương pháp
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
66.

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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
66 để gọi hàm trên mỗi phần tử trong mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18; 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 move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
0

Hợp nhất nhiều mảng với nhau

Ví dụ này sử dụng phương thức

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
69 để hợp nhất mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 với mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
71, để tạo ra một mảng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
72 mới. Lưu ý rằng
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 và
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
71 vẫn không thay đổi.

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
1

Sao 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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
18 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
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
76, sau đó bằng cách sử dụng phương thức
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
77.

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
2

Tất cả các hoạt động sao chép mảng tích hợp (cú pháp lan truyền,

const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
2,
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
01 và
const fruits = ['Apple', 'Banana'];

// The index of an array's first element is always 0.
fruits[0]; // Apple

// The index of an array's second element is always 1.
fruits[1]; // Banana

// The index of an array's last element is always one
// less than the length of the array.
fruits[fruits.length - 1]; // Banana

// Using a index number larger than the array's length
// returns 'undefined'.
fruits[99]; // undefined
3) tạo ra 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
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
81 để chuyển đổi mảng thành chuỗi JSON và sau đó
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
82 để 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 move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
3

Bạn cũng có thể tạo các bản sao sâu bằng phương pháp

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
83, 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 move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
4

Nhóm các yếu tố của một mảng

Các phương thức

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
2 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ó

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
85 và
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
86.

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
5

Để sử dụng

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
87, 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ề

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
86 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ử
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
86 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.

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
6

Lư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

const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
3. Điều này rất giống với
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
91 ngoại trừ việc nó nhóm các phần tử của mảng thành
const fruits = ['Apple', 'Banana'];

fruits.includes('Banana'); // true
fruits.includes('Cherry'); // false

// If indexOf() doesn't return -1, the array contains the given item.
fruits.indexOf('Banana') !== -1; // true
fruits.indexOf('Cherry') !== -1; // false
4 có thể sử dụng giá trị tùy ý (đối tượng hoặc nguyên thủy) làm 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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
93 trong
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
94 lên
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
95. Vị trí cũ tại
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
96 được làm trống.

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
7

Đây là đầu ra:

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
8

Sử dụng một mảng để lập bảng một tập hợp các giá trị

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
9

Kết quả trong

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
0

Tạ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

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
97 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ả về bởi
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
98 và
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
99.

Ví dụ:

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
1

Để biết thêm thông tin về kết quả của một trận đấu, hãy xem trang

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
98 và
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
99.

Ghi chú

// '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
3 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 move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
03 là một thuộc tính (tuy nhiên, cụ thể,
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
04 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ệ:

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
2

Cú 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 move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
05 thay vì
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
06), mặc dù thường không cần thiết.

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
07 trong
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
06 bị ép buộc thành một chuỗi bởi công cụ JavaScript thông qua chuyển đổi
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
03 ngầm. Do đó,
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
10 và
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
11 sẽ đề cập đến hai vị trí khác nhau trên đối tượng
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
12 và ví dụ sau đây có thể là
const fruits = ['Apple', 'Banana'];
const fruitsString = fruits.join(', ');
console.log(fruitsString);
// "Apple, Banana"
5:

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
3

Chỉ

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
05 là một chỉ số mảng thực tế.
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
15 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

// '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
6 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ụ:

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
17,
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
77,
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
20, v.v.) có tính đến giá trị của thuộc tí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
6 của mảng khi chúng được gọi.

Các phương thức khác (ví dụ:

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
29,
if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
34, v.v.) cũng dẫn đến các bản cập nhật cho thuộc tí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
6 của mảng.

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
4

Khi đặ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

// '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
6 của mảng cho phù hợp:

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
5

Tă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
6.

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
6

Giảm thuộc tí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
6, tuy nhiên, xóa các yếu tố.

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
7

Điều này được giải thích thêm trên trang

function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
27.

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à

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
2.

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 move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
29 trước khi truy cập chỉ mục và không kết hợp các khe trống với
const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
2:

  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    69
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    32
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    60
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    61
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    35
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    62
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    66
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    20
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    39
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    63
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    64
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    65
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    43
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    77
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    45
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    46
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    34

Để 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à

const fruits = ['Apple', 'Banana'];
console.log(fruits.indexOf('Banana'));
// 1
2:

  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    49
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    50
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    51
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    52
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    53
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    54
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    87
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    56
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    26
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    17
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    59
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    60
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    14

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 move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
62 để 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
    function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    63,
    function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    64 và
    function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    65 đối tượng): Giá trị của chúng được sao chép vào mảng mới.

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 move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
66:

  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    69
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    61
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    35
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    62
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    63
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    77
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    34 (để xây dựng mảng các phần tử bị loại bỏ đã được trả về)

Lưu ý rằng

if (typeof Array.prototype.move === "undefined") {
  Array.prototype.move = function(from, to, on = 1) {
    return this.splice(to, 0, ...this.splice(from, on)), this
  }
}

[3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
87 và
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
56 không sử dụng
function move(array, from, to, on = 1) {
  return array.splice(to, 0, ...array.splice(from, on)), array
}

move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
66 để 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
// '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
3 đơ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:

  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    32
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    50
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    31
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    29
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    43
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    38
  • function move(array, from, to, on = 1) {
      return array.splice(to, 0, ...array.splice(from, on)), array
    }
    
    move([3, 4, 5, 1, 2], 3, 0, 2) // => [1, 2, 3, 4, 5]
    
    46
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    34
  • if (typeof Array.prototype.move === "undefined") {
      Array.prototype.move = function(from, to, on = 1) {
        return this.splice(to, 0, ...this.splice(from, on)), this
      }
    }
    
    [3, 4, 5, 1, 2].move(3, 0, 2) // => [1, 2, 3, 4, 5]
    
    43

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

// '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
6 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.

function moved(array, from, to, on = 1) {
  return array = array.slice(), array.splice(to, 0, ...array.splice(from, on)), array
}
8

Thông số kỹ thuật

Sự chỉ rõ
Đặ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

Xem thêm