如何从JSON对象中删除索引
在本文中,我们将讨论从JSON对象中删除任何索引的方法。
什么是JSON?
JSON代表JavaScript对象表示法。它是一种用于结构化数据的格式。当数据从服务器发送到网页时,使用此格式。 JSON是“自我描述”且易于理解。它是XML数据交换格式的替代品。与XML相比,它更容易对数据进行结构化。它支持数组和对象等数据结构以及在服务器上快速执行的JSON文档。
JSON语法规则 –
- 数据应该是键值对
- 数据由逗号(“,”)分隔
- 花括号({})表示对象
- 方括号([])表示数组
它用于什么?
像XML一样,它是一种格式化数据的方式之一。当数据从服务器发送到网页时,使用此格式的数据。
先决条件 –
- 已安装Node
语法 –
let 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"
}]
现在,要从这个JSON对象中删除任何索引,我们将学习以下不同的方法:
- 使用splice方法。
- 使用删除属性。
- 使用null。
使用splice()方法: 此方法用于通过删除现有元素和/或添加新元素来修改内容。
在这里,我们只会看如何使用splice来删除元素。
语法:
Object.splice(index, remove_count )
示例 1: 在这个示例中,我们将删除对象中第一个索引位置的元素。
Javascript
let Data_Structures = [
{
"Name": "Trees",
"Course": "Introduction of Trees",
"Content": ["Binary Tree", "BST",
"Generic Tree"]
},
{
"Name": "Graphs",
"Topics": ["BFS", "DFS", "Topological Sort"]
}
]
console.log("Object Before deleting : ");
console.log(Data_Structures);
console.log("Using splice for Deleting object at index 0");
Data_Structures.splice(0, 1);
console.log("Object Post Deleting : ");
console.log(Data_Structures);
如何运行?
- 打开命令提示符。
- 使用cd命令转到保存JSON文件的目录。
- 现在只需键入命令即可。
node file_name
输出:
Object Before deleting :
[
{
Name: 'Trees',
Course: 'Introduction of Trees',
Content: [ 'Binary Tree', 'BST', 'Generic Tree' ]
},
{ Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] }
]
Using splice for Deleting object at index 0
Object Post Deleting :
[ { Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] } ]
使用 delete property: 此关键字用于删除属性及其关联的值。删除后,无法恢复已删除的属性。
语法:
delete object.property or
delete object['property'] or
delete object[index]
返回值: 在所有情况下返回 true,当属性为非可配置的自有属性时返回 false。
示例 2: 我们将使用 delete 关键字删除一个对象以及它的属性。
首先,我们将删除第一个索引处的对象,然后删除索引为1的对象的属性。
Javascript
let students = [
{
"student": "Akshit",
"address": "Moradabad",
"phone": "98760"
},
{
"student": "Nikita",
"address": "Lucknow",
"phone": "98754"
},
{
"student": "Somya",
"address": "Delhi",
"phone": "67878"
},
{
"student": "Eshika",
"address": "Bangalore",
"phone": "67676"
},
{
"student": "Parul",
"address": "Chennai",
"phone": "77668"
}
]
console.log("Object Before deleting : ");
console.log(students);
console.log("Using Delete Keyword for "
+ "Deleting object at index 0");
delete students[0];
console.log("Object Post Deleting : ");
console.log(students);
console.log("Using Delete Keyword for "
+ "Deleting object property at index 1");
delete students[1].phone;
console.log("Object Post Deleting : ");
console.log(students);
输出:
Object Before deleting :
[
{ student: 'Akshit', address: 'Moradabad', phone: '98760' },
{ student: 'Nikita', address: 'Lucknow', phone: '98754' },
{ student: 'Somya', address: 'Delhi', phone: '67878' },
{ student: 'Eshika', address: 'Bangalore', phone: '67676' },
{ student: 'Parul', address: 'Chennai', phone: '77668' }
]
Using Delete Keyword for Deleting object at index 0
Object Post Deleting :
[
<1 empty item>,
{ student: 'Nikita', address: 'Lucknow', phone: '98754' },
{ student: 'Somya', address: 'Delhi', phone: '67878' },
{ student: 'Eshika', address: 'Bangalore', phone: '67676' },
{ student: 'Parul', address: 'Chennai', phone: '77668' }
]
Using Delete Keyword for Deleting object property at index 1
Object Post Deleting :
[
<1 empty item>,
{ student: 'Nikita', address: 'Lucknow' },
{ student: 'Somya', address: 'Delhi', phone: '67878' },
{ student: 'Eshika', address: 'Bangalore', phone: '67676' },
{ student: 'Parul', address: 'Chennai', phone: '77668' }
]
使用null : 当我们将任何对象的值设置为null时,节点的垃圾回收器会自动删除该属性的值。然而,属性的名称仍然保留。
语法:
object.property=null
示例3: 在这个示例中,我们将使用null来删除一个对象的属性。
Javascript
let Data_Structures = [
{
"Name": "Trees",
"Course": "Introduction of Trees",
"Content": ["Binary Tree", "BST",
"Generic Tree"]
},
{
"Name": "Graphs",
"Topics": ["BFS", "DFS", "Topological Sort"]
}
]
console.log("Object Before deleting : ");
console.log(Data_Structures);
console.log("Using null for Deleting object at index 0");
Data_Structures[0].Content = null;
console.log("Object Post Deleting : ");
console.log(Data_Structures);
输出:
Object Before deleting :
[
{
Name: 'Trees',
Course: 'Introduction of Trees',
Content: [ 'Binary Tree', 'BST', 'Generic Tree' ]
},
{ Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] }
]
Using null for Deleting object at index 0
Object Post Deleting :
[
{ Name: 'Trees', Course: 'Introduction of Trees', Content: null },
{ Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] }
]
极客教程