Một đối tượng trong JavaScript là tập hợp các cặp khóa-giá trị. Một trong những cặp khóa-giá trị này được gọi là thuộc tính đối tượng. Cả khóa và giá trị của thuộc tính đều có thể thuộc bất kỳ loại dữ liệu nào - Số, Chuỗi, Mảng, Đối tượng, v.v.
Ví dụ
const dog = {
name: "Sandy",
age: 3,
emoji: "🐶"
}
Ở đây,
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
7, if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
8 và if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
9 là các thuộc tính của đối tượng con chóTrong bài viết này, chúng ta sẽ xem xét một số cách để xóa thuộc tính khỏi Đối tượng và so sánh chúng để hiểu phương pháp nào phù hợp trong ngữ cảnh nhất định
Loại bỏ một thuộc tính khỏi một đối tượng
Toán tử xóa
Cách chính xác về mặt ngữ nghĩa để xóa một thuộc tính khỏi một đối tượng là toán tử
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0. Hãy xem nó hoạt độngTrong ví dụ trên, toán tử
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0 được sử dụng để loại bỏ các thuộc tính if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
2 và if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
3 khỏi đối tượng if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0Cố gắng truy cập bất kỳ thuộc tính nào đã bị xóa sẽ trả về
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
1Ngoài ra, toán tử
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0 trả về một giá trị boolean cho biết việc xóa có thành công hay khôngif [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
Nếu chúng tôi chạy mã này, vì thuộc tính đã bị xóa, chúng tôi được chào đón với
Failed to remove 'age' property, perhaps it doesn't exist?
Đối tượng mới không có thuộc tính
Nếu chúng ta không muốn sửa đổi một đối tượng tại chỗ, nhưng cũng muốn một phiên bản của đối tượng đó không có thuộc tính cụ thể, thì chúng ta có thể tạo một đối tượng khác có tất cả các thuộc tính giống nhau ngoại trừ một thuộc tính.
Trong trường hợp chúng ta biết tên của thuộc tính muốn xóa, chúng ta chỉ cần sử dụng tính năng hủy đối tượng để giải nén đối tượng thành 2 phần
- Thuộc tính chúng tôi muốn xóa
- Một đối tượng đại diện cho phần còn lại của đối tượng
Hãy xem hướng dẫn thực hành, thực tế của chúng tôi để học Git, với các phương pháp hay nhất, tiêu chuẩn được ngành chấp nhận và bao gồm bảng gian lận. Dừng các lệnh Git trên Google và thực sự tìm hiểu nó
Tuy nhiên, nếu chúng tôi không biết tên chính xác của sản phẩm mà chúng tôi muốn xóa, chúng tôi sẽ cần tạo một bộ lọc để kiểm tra xem một sản phẩm có đáp ứng tiêu chí xóa hay không
Ví dụ: hãy xóa tất cả các thuộc tính có giá trị số
Hàm giảm []
Ngoài ra, chúng ta có thể sử dụng phương thức
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
3, đây là một phương thức mảng tích hợp, lấy tập hợp và hàm rút gọn làm đối sốSau đó, hàm này lặp qua tất cả các phần tử trong bộ sưu tập và sửa đổi bộ tích lũy [mà bạn có thể coi là kết quả tạm thời cho mỗi bước] và trả về nó. Hãy xem phương pháp này hoạt động
Phần kết luận
Trong bài viết này, chúng ta đã thấy cách xóa một thuộc tính khỏi một đối tượng theo một số cách. Chúng tôi đã thấy rằng việc sử dụng
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0 sẽ thay đổi đối tượng. Vì vậy, chúng ta đã thảo luận về một số cách - cú pháp if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
5 và phương thức if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
3, để xóa một thuộc tính khỏi một đối tượng mà không làm biến đổi nóCâu hỏi kinh điển này thỉnh thoảng lại xuất hiện. Ngay cả người tạo ra Node. js Ryan Dahl đã hỏi câu hỏi này từ khán giả trong Node của anh ấy. js [nhân tiện, đây là một bản trình bày xuất sắc]
Làm cách nào để xóa phần tử khỏi mảng?
Toán tử
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0 có được sử dụng không? Sử dụng mối nối [] để xóa mục tùy ý
Cách chính xác để xóa một mục khỏi mảng là sử dụng
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
7. Phải mất một chỉ mục và số lượng mục cần xóa bắt đầu từ chỉ mục đó________số 8
Đừng nhầm lẫn điều này với người anh em họ tương tự của nó là
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
8 được sử dụng để trích xuất một phần của mảngSử dụng shift[] để xóa từ đầu
Nếu bạn luôn quan tâm đến việc xóa mục đầu tiên hoặc mục cuối cùng, thì bạn có một giải pháp thay thế. Các phương pháp
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
1 và > let array = ["a", "b", "c"];
> array.shift[];
'a'
> array;
[ 'b', 'c' ]
0 tồn tại chính xác cho mục đích này. Với if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
1, bạn có thể xóa mục đầu tiên> let array = ["a", "b", "c"];
> array.shift[];
'a'
> array;
[ 'b', 'c' ]
Sử dụng pop[] để xóa từ cuối
Và với
> let array = ["a", "b", "c"];
> array.shift[];
'a'
> array;
[ 'b', 'c' ]
0, bạn có thể xóa mục cuối cùngif [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
2Sử dụng xóa tạo các điểm trống
Dù bạn làm gì, đừng sử dụng
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0 để xóa một mục khỏi mảng. Ngôn ngữ JavaScript chỉ định rằng các mảng thưa thớt, tôi. e. , họ có thể có lỗ hổng trong họSử dụng
if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0 tạo ra các loại lỗ này. Nó xóa một mục khỏi mảng, nhưng nó không cập nhật thuộc tính độ dài. Điều này khiến mảng ở trạng thái buồn cười tốt nhất nên tránhif [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
3Lưu ý chỗ trống và chiều dài không thay đổi
Nhớ điều này
Lần tới khi bạn cần xóa thứ gì đó khỏi mảng, hãy ghi nhớ những điều sau
Xóa?Một mục> let array = ["a", "b", "c"];
> array.shift[];
'a'
> array;
[ 'b', 'c' ]
5Mục đầu tiên> let array = ["a", "b", "c"];
> array.shift[];
'a'
> array;
[ 'b', 'c' ]
6Mục cuối cùng> let array = ["a", "b", "c"];
> array.shift[];
'a'
> array;
[ 'b', 'c' ]
7Còn xóa thì sao?Cố gắng tránh if [delete student.age] {
console.log["Removed 'age' property from student"];
} else {
console.log["Failed to remove 'age' property, perhaps it doesn't exist?"];
}
0, gây ra các mảng thưa thớt. Các phương thức JavaScript để xóa một phần tử khỏi một mảngNút không đợi cuộc gọi cơ sở dữ liệu của bạn kết thúc?
Tìm hiểu cách cuộc gọi không đồng bộ hoạt động và làm cho ứng dụng của bạn chạy như bạn dự định. Nhận khóa học email ngắn về tính không đồng bộ và hai chương từ Hoàn thiện ứng dụng nút của bạn