Javascript:  Splice vs Slice

Javascript: Splice vs Slice

Trong JavaScript, spliceslice là hai ph??ng th?c ???c s? d?ng ?? làm vi?c v?i m?ng, nh?ng chúng có m?c ?ích và cách ho?t ??ng khác nhau.

1. Splice

Splice ???c s? d?ng ?? thay ??i m?ng g?c. B?n có th? thêm, xóa ho?c thay th? các ph?n t? trong m?ng.

Cú pháp c?a splice:


Sao chép m?

arr.splice(start, deleteCount, item1, item2, ..., itemN);

  • start: V? trí b?t ??u thay ??i trong m?ng.
  • deleteCount: S? l??ng ph?n t? c?n xóa, tùy ch?n.
  • item1, item2, ...: Các ph?n t? s? ???c thêm vào m?ng sau v? trí start.

Ví d?:


Sao chép m?

const arr = ['apple', 'banana', 'mango', 'orange']; // Xóa 'banana' và 'mango', sau ?ó thêm 'grape' vào v? trí ?ó arr.splice(1, 2, 'grape'); console.log(arr); // ['apple', 'grape', 'orange']

Trong ví d? trên, m?ng g?c ?? b? thay ??i: 'banana''mango' b? xóa, 'grape' ???c thêm vào.

2. Slice

Slice ???c s? d?ng ?? sao chép m?t ph?n m?ng mà kh?ng làm thay ??i m?ng g?c. Nó tr? v? m?t m?ng m?i ch?a các ph?n t? t? v? trí b?t ??u ??n v? trí k?t thúc (kh?ng bao g?m v? trí k?t thúc).

Cú pháp c?a slice:


Sao chép m?

arr.slice(start, end);

  • start: V? trí b?t ??u c?t (bao g?m).
  • end: V? trí k?t thúc c?t (kh?ng bao g?m).

Ví d?:


Sao chép m?

const arr = ['apple', 'banana', 'mango', 'orange']; // Sao chép các ph?n t? t? v? trí 1 ??n 3 (kh?ng bao g?m 3) const newArr = arr.slice(1, 3); console.log(newArr); // ['banana', 'mango'] console.log(arr); // ['apple', 'banana', 'mango', 'orange']

? ?ay, m?ng g?c kh?ng b? thay ??i, mà ch? t?o ra m?t m?ng m?i t? m?ng c?.

Tóm l?i:

  • Splice: Thay ??i m?ng g?c, có th? xóa, thêm ho?c thay th? ph?n t?.
  • Slice: Sao chép m?t ph?n m?ng mà kh?ng làm thay ??i m?ng g?c.

Nh? r?ng, splice thích h?p khi b?n c?n thay ??i m?ng tr?c ti?p, còn slice h?u ích khi b?n ch? c?n sao chép m?t ph?n c?a m?ng.

要查看或添加评论,请登录

Thi?u Quang Khoa的更多文章

  • JavaScript Interview Questions (Part 4)

    JavaScript Interview Questions (Part 4)

    61 What are the main rules of promise 62 What is callback in callback 63 What is promise chaining 64 What is…

  • JavaScript Interview Questions (Part 3)

    JavaScript Interview Questions (Part 3)

    41 What are the differences between cookie, local storage and session storage 42 What is the main difference between…

  • Top 400 JavaScript Interview Questions (Part 2)

    Top 400 JavaScript Interview Questions (Part 2)

    21 What is the Temporal Dead Zone 22 What is an IIFE (Immediately Invoked Function Expression) 23 How do you decode or…

  • Top 400 JavaScript Interview Questions (Part 1)

    Top 400 JavaScript Interview Questions (Part 1)

    No. Questions 1 What are the possible ways to create objects in JavaScript 2 What is a prototype chain 3 What is the…

  • Javascript: isFunctions (Part 2)

    Javascript: isFunctions (Part 2)

    5. isUndefined Hàm này ki?m tra xem giá tr? có ph?i là hay kh?ng.

    1 条评论
  • Javascript: isFunctions (Part 1)

    Javascript: isFunctions (Part 1)

    1. isNumber Hàm này ki?m tra xem giá tr? có thu?c ki?u và kh?ng ph?i là hay kh?ng.

    2 条评论
  • Javascript Interview Questions

    Javascript Interview Questions

    5 features of javascript What is DOM? Data types in javascript? Difference between let, var, const Difference between…

  • JavaScript Reversed Words

    JavaScript Reversed Words

    All Javascript Reserved Keywords The below list cannot be used as variable names, as they are reserved keywords. Most…

  • Using parseInt() and toString() for conversions

    Using parseInt() and toString() for conversions

    Trong JavaScript, vi?c chuy?n ??i gi?a các ??nh d?ng s? là m?t tác v? ph? bi?n, và có nhi?u ph??ng pháp ?? th?c hi?n…

  • JavaScript Events

    JavaScript Events

    Các s? ki?n HTML là nh?ng "vi?c" x?y ra ??i v?i các ph?n t? HTML. Khi JavaScript ???c s? d?ng trong các trang HTML…