JSON là viết tắt của Ký hiệu đối tượng JavaScript. Nó là một định dạng để cấu trúc dữ liệu. Định dạng này được sử dụng khi dữ liệu được gửi từ máy chủ đến trang web. JSON là "tự mô tả" và dễ hiểu. Nó là sự thay thế của định dạng trao đổi dữ liệu XML. Thật dễ dàng để cấu trúc dữ liệu so với XML. Nó hỗ trợ các cấu trúc dữ liệu như mảng và đối tượng cũng như các tài liệu JSON được thực thi nhanh chóng trên máy chủ
Quy tắc cú pháp JSON –
- Dữ liệu phải ở trong khóa. cặp giá trị
- Dữ liệu cách nhau bởi dấu phẩy[‘,’]
- Dấu ngoặc nhọn { } giữ đối tượng
- Dấu ngoặc vuông [ ] giữ mảng
Cái này được dùng để làm gì?
Giống như XML, đây là một trong những cách định dạng dữ liệu. Định dạng dữ liệu như vậy được sử dụng khi dữ liệu được gửi từ máy chủ đến trang web
Điều kiện tiên quyết -
- Đã cài đặt nút
Cú pháp –
var json_object = [{ "property_1" : "property_value_1", "property_2" : "property_value_2" }, { "property_1" : "property_value_1", "property_2" : "property_value_2", "property_3" : "property_value_3" }]
Bây giờ, để xóa bất kỳ chỉ mục nào khỏi đối tượng JSON này, chúng ta sẽ tìm hiểu các phương thức khác nhau được giải thích bên dưới –
- Sử dụng phương pháp nối
- Sử dụng thuộc tính Xóa
- sử dụng null
Sử dụng phương thức Splice[]. Phương pháp này được sử dụng để sửa đổi nội dung bằng cách loại bỏ các phần tử hiện có và/hoặc bằng cách thêm các phần tử mới
Ở đây, chúng ta sẽ chỉ xem xét cách chúng ta có thể sử dụng mối nối để loại bỏ các phần tử
cú pháp.
Object.splice[index, remove_count ]
Tham số. Phương thức này chấp nhận hai hoặc ba tham số, một số tham số được mô tả bên dưới.
mục lục. Nó là một tham số bắt buộc. Tham số này là chỉ số bắt đầu sửa đổi mảng [có gốc là 0]. Điều này cũng có thể là tiêu cực, bắt đầu sau nhiều yếu tố đó, tính từ cuối
remove_count. Số phần tử cần xóa khỏi chỉ mục bắt đầu
Giá trị trả về. Mặc dù nó thay đổi mảng ban đầu tại chỗ, nhưng nó vẫn trả về danh sách các mục đã xóa. Trong trường hợp không có mảng nào bị loại bỏ, nó sẽ trả về một mảng trống
ví dụ 1. Trong ví dụ này, chúng ta sẽ loại bỏ phần tử ở chỉ mục đầu tiên của đối tượng
Javascript
Object.splice[index, remove_count ]56
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]58
Object.splice[index, remove_count ]59
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]1
Object.splice[index, remove_count ]2
Object.splice[index, remove_count ]3
Object.splice[index, remove_count ]4
Object.splice[index, remove_count ]5
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2
Object.splice[index, remove_count ]8
Object.splice[index, remove_count ]4
Object.splice[index, remove_count ]20
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2______123
Object.splice[index, remove_count ]24
Object.splice[index, remove_count ]25
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]27
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]29
Object.splice[index, remove_count ]20
Object.splice[index, remove_count ]21
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]23
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]1
Object.splice[index, remove_count ]2
Object.splice[index, remove_count ]3
Object.splice[index, remove_count ]4
Object.splice[index, remove_count ]29
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2______342
Object.splice[index, remove_count ]24
Object.splice[index, remove_count ]44
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]46
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]48
Object.splice[index, remove_count ]21
Object.splice[index, remove_count ]0____431
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]21
_______157____435____436
Object.splice[index, remove_count ]37
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]39
_______157____435____1562
Object.splice[index, remove_count ]37
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]565
_______157____435____1568
Object.splice[index, remove_count ]37
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]39
Object.splice[index, remove_count ]572
Làm thế nào để chạy này?
- Mở dấu nhắc lệnh
- Chuyển đến thư mục lưu tệp JSON của bạn bằng cách sử dụng lệnh cd
- Bây giờ chỉ cần gõ lệnh
Object.splice[index, remove_count ]2
đầu ra
Sử dụng thuộc tính xóa. Từ khóa này được sử dụng để xóa thuộc tính cũng như giá trị liên quan của nó. Đã xóa bài đăng, tài sản đã xóa không thể lấy lại được
cú pháp
Object.splice[index, remove_count ]2
Thông số
Object.splice[index, remove_count ]4
Giá trị trả về. Nó trả về true cho tất cả các trường hợp và trả về false khi thuộc tính là thuộc tính riêng không thể định cấu hình
ví dụ 2. Chúng tôi sẽ sử dụng từ khóa xóa để xóa một đối tượng cũng như thuộc tính của nó.
Đầu tiên ta sẽ xóa một đối tượng ở chỉ mục đầu tiên sau đó xóa thuộc tính của đối tượng ở chỉ mục 1
Javascript
Object.splice[index, remove_count ]56
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]58
Object.splice[index, remove_count ]576
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]1
Object.splice[index, remove_count ]2____1580
Object.splice[index, remove_count ]4
Object.splice[index, remove_count ]582
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1585____14
Object.splice[index, remove_count ]587
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1590____14
Object.splice[index, remove_count ]592
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]23
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]1
Object.splice[index, remove_count ]2____1580
Object.splice[index, remove_count ]4
Object.splice[index, remove_count ]00
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1585______14
Object.splice[index, remove_count ]05
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1590____14
Object.splice[index, remove_count ]10
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]23
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]1
Object.splice[index, remove_count ]2____1580
Object.splice[index, remove_count ]4
Object.splice[index, remove_count ]18
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1585______14
Object.splice[index, remove_count ]23
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1590____14
Object.splice[index, remove_count ]28
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]23
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]1
Object.splice[index, remove_count ]2____1580
Object.splice[index, remove_count ]4
Object.splice[index, remove_count ]36
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1585____14
Object.splice[index, remove_count ]41
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1590____14
Object.splice[index, remove_count ]46
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]23
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]1
Object.splice[index, remove_count ]2____1580
Object.splice[index, remove_count ]4
Object.splice[index, remove_count ]54
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1585____14
Object.splice[index, remove_count ]59
Object.splice[index, remove_count ]6
Object.splice[index, remove_count ]2____1590____14
Object.splice[index, remove_count ]64
Object.splice[index, remove_count ]0____431
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]21
_______157____435____436
Object.splice[index, remove_count ]37
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]74
Object.splice[index, remove_count ]57____435
Object.splice[index, remove_count ]77
Object.splice[index, remove_count ]0____179____180
Object.splice[index, remove_count ]37
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]83
Object.splice[index, remove_count ]84
_______157____435____1568
Object.splice[index, remove_count ]37
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]74
Object.splice[index, remove_count ]57____435
Object.splice[index, remove_count ]77
Object.splice[index, remove_count ]0
Object.splice[index, remove_count ]79____196
Object.splice[index, remove_count ]37
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]83
Object.splice[index, remove_count ]200
_______157____435____1568
Object.splice[index, remove_count ]37
Object.splice[index, remove_count ]57
Object.splice[index, remove_count ]74
Object.splice[index, remove_count ]572
đầu ra
sử dụng null. Khi chúng ta đặt giá trị của bất kỳ đối tượng nào thành null, thì bộ thu gom rác của nút sẽ tự động xóa giá trị thuộc tính. Tuy nhiên, tên của tài sản vẫn còn