how to remove json object key and value.?
我有一个JSON对象,如下所示。在这里,我想删除"OtherIndustry"条目及其值,方法是使用下面不起作用的代码。
1 | var updatedjsonobj = delete myjsonobj['otherIndustry']; |
如何删除JSON对象特定的键及其值。下面是我的示例JSON对象,我想删除"OtherIndustry"键及其值。
1 2 3 4 5 6 7 8 9 10 11 12 13 | var myjsonobj = { "employeeid":"160915848", "firstName":"tet", "lastName":"test", "email":"[email protected]", "country":"Brasil", "currentIndustry":"aaaaaaaaaaaaa", "otherIndustry":"aaaaaaaaaaaaa", "currentOrganization":"test", "salary":"1234567" }; delete myjsonobj ['otherIndustry']; console.log(myjsonobj); |
如果日志仍然打印同一对象,而不从该对象中删除"OtherIndustry"条目。
在其他的手,executes
How to remove Json object specific key and its value ?
你只需要知道,房地产是为了删除它从对象的属性。
1 | delete myjsonobj['otherIndustry']; |
1 2 3 4 5 6 7 8 9 10 11 12 13 | let myjsonobj = { "employeeid":"160915848", "firstName":"tet", "lastName":"test", "email":"[email protected]", "country":"Brasil", "currentIndustry":"aaaaaaaaaaaaa", "otherIndustry":"aaaaaaaaaaaaa", "currentOrganization":"test", "salary":"1234567" } delete myjsonobj['otherIndustry']; console.log(myjsonobj); |
如果你想删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | let value="test"; let myjsonobj = { "employeeid":"160915848", "firstName":"tet", "lastName":"test", "email":"[email protected]", "country":"Brasil", "currentIndustry":"aaaaaaaaaaaaa", "otherIndustry":"aaaaaaaaaaaaa", "currentOrganization":"test", "salary":"1234567" } Object.keys(myjsonobj).forEach(function(key){ if(myjsonobj[key]==value) delete myjsonobj[key]; }); console.log(myjsonobj); |
它可以跟踪这样做,你看:
1 2 3 4 5 6 7 8 | var obj = { Objone: 'one', Objtwo: 'two' }; var key ="Objone"; delete obj[key]; console.log(obj); // prints {"objtwo": two} |
这里是一个更多的例子。(检查参考)
1 2 3 4 5 6 7 8 9 10 11 12 13 | const myObject = { "employeeid":"160915848", "firstName":"tet", "lastName":"test", "email":"[email protected]", "country":"Brasil", "currentIndustry":"aaaaaaaaaaaaa", "otherIndustry":"aaaaaaaaaaaaa", "currentOrganization":"test", "salary":"1234567" }; const {otherIndustry, ...otherIndustry2} = myObject; console.log(otherIndustry2); |
1 2 3 4 | .as-console-wrapper { max-height: 100% !important; top: 0; } |