Hỏi về method splice() trong JavaScript

Có thể mình hỏi 1 câu hơi ngu ngục nhưng mình đang học lập trình và thấy khó chịu về sự lằng nhằng của nó:

Dùng arr.splice() theo như mình hiểu thì là sẽ thêm, bớt, thay đổi được arr.
VD: mình có mảng

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

nếu mình code

arr.splice(0,2);
console.log(arr);

thì sẽ ra được mảng arr = [2,3,4,5]

Nhưng nếu gán giá trị mảng cho 1 biến khác, VD :

let brr = arr.splice(0,2)

thì lại ra kết quả là

brr = [0,1]

Ae có thể giải thích cho mình tại sao lại thế ko ạ? Many thanks!

1 Like

Trước hết bạn nên đọc docs về Array.prototype.splice() đã.

Syntax

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

Parameters

start

The index at which to start changing the array.

If greater than the length of the array, start will be set to the length of the array. In this case, no element will be deleted but the method will behave as an adding function, adding as many element as item[n*] provided.

If negative, it will begin that many elements from the end of the array. (In this case, the origin -1 , meaning -n is the index of the n th last element, and is therefore equivalent to the index of array.length - n .) If array.length + start is less than 0 , it will begin from index 0 .

deleteCount Optional

An integer indicating the number of elements in the array to remove from start .

If deleteCount is omitted, or if its value is equal to or larger than array.length - start (that is, if it is equal to or greater than the number of elements left in the array, starting at start ), then all the elements from start to the end of the array will be deleted.

If deleteCount is 0 or negative, no elements are removed. In this case, you should specify at least one new element (see below).

item1, item2, ... Optional

The elements to add to the array, beginning from start . If you do not specify any elements, splice() will only remove elements from the array.

Return value

An array containing the deleted elements.
If only one element is removed, an array of one element is returned.
If no elements are removed, an empty array is returned.

Bạn sử dụng splice với 2 tham số, vậy bạn bắt đầu xoá 2 phần tử từ phần tử 0. Giá trị trả về chính là danh sách các phần tử bị xoá khỏi arr. Mảng arr vẫn bị xoá bình thường, còn bạn có lưu kết quả của method splice() hay không cũng không quan trọng.

7 Likes

Nói nôm na vậy cho dễ hiểu: Dòng “arr.splice(0,2)” là bạn đang thực hiện việc remove bớt các phần tử trong arr. Nên sang dòng tiếp theo “console.log(arr)” thì chỉ trả về giá trị sau cùng của mảng arr (tức là mảng đã được bỏ đi 2 phần tử đầu tiên).

Hàm splice sẽ trả về những giá trị bị bỏ đi khi bạn gán biến vô cho nó, nên biến brr sẽ chứa những giá trị bị bỏ đi khi thực hiện “arr.plice(0,2)” (tức là brr chỉ chứa hai giá trị đầu tiên).

3 Likes

“The splice() method adds/removes items to/from an array, and returns the removed item(s).”
Splice thay đổi arr gốc: arr.splice(0,2) // thực hiện việc xóa 2 item ==> arr bị xóa 2 item ==> khi bạn console.log(arr) được arr = [2,3,4,5]
Splice return những item bạn xóa ===> brr = arr.splice(0, 2) nên brr = [0 ,1]

3 Likes

đây là hiện tượng khá phổ biến, viết code mà không đọc document, không biết mình đang viết gì

9 Likes

Nó tiện lợi chứ không lằng nhằng. Phương thức đã làm đúng mục đích.
Câu hỏi đơn giản: Bạn muốn xem lại các phần tử đã xóa thì làm thế nào? NẾU kết quả trả về là mảng gốc (đã xóa) chứ không phải mảng chứa các phần tử đã xóa.

7 Likes

1 tiếng debug giúp tiết kiệm 5 phút đọc tài liệu :joy:

5 Likes

thanks bro toi hieu roi

1 Like

Cám ơn ae nhé tôi hiểu rồi. Lúc đấy đang cháy bugy, giờ nghe ae nói mới thấy mình học sót nhiều quá. Many thanks~

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?