Mảng javascript tìm và thay thế

Phương thức

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
1 thay đổi nội dung của một mảng bằng cách loại bỏ hoặc thay thế các phần tử hiện có và/hoặc thêm các phần tử mới vào vị trí. Để truy cập một phần của mảng mà không sửa đổi nó, hãy xem
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
2

Thử nó

cú pháp

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)

Thông số

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
3

Chỉ mục dựa trên số không để bắt đầu thay đổi mảng, được chuyển đổi thành số nguyên

  • Chỉ số âm đếm ngược từ cuối mảng — nếu sử dụng
    const myFish = ["angel", "clown", "mandarin", "sturgeon"];
    const removed = myFish.splice(2, 0, "drum");
    
    // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
    // removed is [], no elements removed
    
    4,
    const myFish = ["angel", "clown", "mandarin", "sturgeon"];
    const removed = myFish.splice(2, 0, "drum");
    
    // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
    // removed is [], no elements removed
    
    5
  • Nếu
    const myFish = ["angel", "clown", "mandarin", "sturgeon"];
    const removed = myFish.splice(2, 0, "drum");
    
    // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
    // removed is [], no elements removed
    
    6 hoặc
    const myFish = ["angel", "clown", "mandarin", "sturgeon"];
    const removed = myFish.splice(2, 0, "drum");
    
    // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
    // removed is [], no elements removed
    
    3 bị bỏ qua, thì sử dụng
    const myFish = ["angel", "clown", "mandarin", "sturgeon"];
    const removed = myFish.splice(2, 0, "drum");
    
    // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
    // removed is [], no elements removed
    
    8
  • Nếu
    const myFish = ["angel", "clown", "mandarin", "sturgeon"];
    const removed = myFish.splice(2, 0, "drum");
    
    // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
    // removed is [], no elements removed
    
    9, sẽ không có phần tử nào bị xóa, nhưng phương thức sẽ hoạt động như một hàm thêm, thêm bao nhiêu phần tử tùy ý
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
00 Tùy chọn

Một số nguyên cho biết số phần tử trong mảng cần loại bỏ khỏi

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
3

Nếu bỏ qua

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
00 hoặc nếu giá trị của nó lớn hơn hoặc bằng số phần tử sau vị trí được chỉ định bởi
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
3 thì tất cả các phần tử từ
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
3 đến cuối mảng sẽ bị xóa. Tuy nhiên, nếu bạn muốn chuyển bất kỳ tham số
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
05 nào, bạn nên chuyển
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
06 thành
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
00 để xóa tất cả các phần tử sau
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
3, bởi vì một
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
09 rõ ràng được chuyển đổi thành
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
8

Nếu

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
00 là
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
8 hoặc âm, không có phần tử nào bị xóa. Trong trường hợp này, bạn nên chỉ định ít nhất một phần tử mới (xem bên dưới)

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
33, …,
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
05 Tùy chọn

Các phần tử để thêm vào mảng, bắt đầu từ

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
3

Nếu bạn không chỉ định bất kỳ phần tử nào, thì

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
1 sẽ chỉ xóa phần tử khỏi mảng

Giá trị trả về

Một mảng chứa các phần tử đã bị xóa

Nếu chỉ một phần tử bị xóa, một mảng của một phần tử sẽ được trả về

Nếu không có phần tử nào bị xóa, một mảng trống sẽ được trả về

Sự miêu tả

Phương pháp

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
1 là một phương pháp đột biến. Nó có thể thay đổi nội dung của
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
38. Nếu số lượng phần tử được chỉ định để chèn khác với số lượng phần tử bị xóa, thì ____239 của mảng cũng sẽ bị thay đổi. Đồng thời, nó sử dụng
const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
00 để tạo một cá thể mảng mới được trả về

Nếu phần bị xóa thưa thớt, thì mảng được trả về bởi

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
1 cũng thưa thớt, với các chỉ số tương ứng đó là các vị trí trống

Phương pháp

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
1 là chung chung. Nó chỉ mong đợi giá trị
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
38 có thuộc tính
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
39 và các thuộc tính có khóa số nguyên. Mặc dù các chuỗi cũng giống như mảng, nhưng phương pháp này không phù hợp để áp dụng cho chúng, vì các chuỗi là bất biến

ví dụ

Xóa các phần tử 0 (không) trước chỉ mục 2 và chèn "trống"

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed

Xóa các phần tử 0 (không) trước chỉ mục 2 và chèn "trống" và "guitar"

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
0

Xóa 1 phần tử tại chỉ số 3

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
3

Xóa 1 phần tử ở chỉ số 2 và chèn "kèn"

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
0

Xóa 2 phần tử khỏi chỉ số 0 và chèn "vẹt", "hải quỳ" và "xanh lam"

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
9

Xóa 2 phần tử, bắt đầu từ chỉ mục 2

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
0

Xóa 1 phần tử khỏi chỉ mục -2

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
1

Xóa tất cả các phần tử, bắt đầu từ chỉ mục 2

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
2

Sử dụng mối nối () trên các mảng thưa thớt

Phương thức

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
1 bảo toàn độ thưa thớt của mảng

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
4

Gọi mối nối () trên các đối tượng không phải mảng

Phương thức

const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is [], no elements removed
1 đọc thuộc tính
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
39 của
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
38. Sau đó, nó cập nhật các thuộc tính có khóa số nguyên và thuộc tính
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
39 nếu cần

Làm cách nào để tìm và thay thế trong mảng JavaScript?

Để thay thế một phần tử trong mảng. Sử dụng phương thức indexOf() để lấy chỉ mục của phần tử bạn muốn thay thế. Gọi mảng. splice() để thay thế phần tử tại chỉ mục cụ thể. Phần tử mảng sẽ được thay thế tại chỗ.

Làm cách nào để thay thế một từ trong mảng JavaScript?

Phương thức thay thế () tìm kiếm một chuỗi cho một giá trị hoặc biểu thức chính quy. Phương thức replace() trả về một chuỗi mới với (các) giá trị được thay thế

Thay thế có hoạt động trên mảng không?

Hàm array_replace() thay thế các giá trị của mảng đầu tiên bằng các giá trị của các mảng tiếp theo . Mẹo. Bạn có thể gán một mảng cho hàm hoặc bao nhiêu mảng tùy thích. Nếu một khóa từ mảng1 tồn tại trong mảng2, các giá trị từ mảng1 sẽ được thay thế bằng các giá trị từ mảng2.

Làm cách nào để thay đổi giá trị cụ thể trong mảng JavaScript?

Thay đổi giá trị của tất cả các phần tử trong mảng. .
Sử dụng phương thức forEach() để lặp lại mảng. Phương thức nhận một hàm được gọi với phần tử mảng, chỉ mục của nó và chính mảng đó
Sử dụng chỉ mục của lần lặp hiện tại để thay đổi phần tử mảng tương ứng