删除 JSON 元素 - JavaScript?
让我们假设下面是我们的 JSON 字符串 −
var details = [ { customerName: "Chris", customerAge: 32 }, { customerName: "David", customerAge: 26 }, { customerName: "Bob", customerAge: 29 }, { customerName: "Carol", customerAge: 25 } ]
要删除 JSON 元素,请使用 JavaScript 中的 delete 关键字。
示例
以下是删除 JSON 元素的完整代码 −
var details = [ { customerName: "Chris", customerAge: 32 }, { customerName: "David", customerAge: 26 }, { customerName: "Bob", customerAge: 29 }, { customerName: "Carol", customerAge: 25 } ] delete details[0].customerAge; console.log(details);
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里,我的文件名是 demo211.js −
PS C:\Users\Amit\JavaScript-code> node demo211.js [ { customerName: 'Chris' }, { customerName: 'David', customerAge: 26 }, { customerName: 'Bob', customerAge: 29 }, { customerName: 'Carol', customerAge: 25 } ]
广告