[英]how to remove json object key and value.?
I have a json object as shown below.我有一个 json 对象,如下所示。 where i want to delete the "otherIndustry" entry and its value by using below code which doesn't worked.
我想通过使用以下不起作用的代码删除“otherIndustry”条目及其值。
var updatedjsonobj = delete myjsonobj['otherIndustry'];
How to remove Json object specific key and its value.如何删除 Json 对象特定的键及其值。 Below is my example json object where i want to remove "otherIndustry" key and its value.
下面是我的示例 json 对象,我想在其中删除“otherIndustry”键及其值。
var myjsonobj = {
"employeeid": "160915848",
"firstName": "tet",
"lastName": "test",
"email": "test@email.com",
"country": "Brasil",
"currentIndustry": "aaaaaaaaaaaaa",
"otherIndustry": "aaaaaaaaaaaaa",
"currentOrganization": "test",
"salary": "1234567"
};
delete myjsonobj ['otherIndustry'];
console.log(myjsonobj);
where the log still prints the same object without removing 'otherIndustry' entry from the object.日志仍然打印相同的对象而不从对象中删除“otherIndustry”条目。
delete
operator is used to remove
an object property
. delete
运算符用于remove
对象property
。
delete
operator does not returns the new object, only returns a boolean
: true or false . delete
运算符不返回新对象,只返回boolean
: true或false 。
In the other hand, after interpreter executes var updatedjsonobj = delete myjsonobj['otherIndustry'];
另一方面,解释器执行后
var updatedjsonobj = delete myjsonobj['otherIndustry'];
, updatedjsonobj
variable will store a boolean
value. ,
updatedjsonobj
变量将存储一个boolean
值。
How to remove Json object specific key and its value ?
如何删除 Json 对象特定的键及其值?
You just need to know the property name in order to delete it from the object's properties.您只需要知道属性名称即可将其从对象的属性中删除。
delete myjsonobj['otherIndustry'];
let myjsonobj = { "employeeid": "160915848", "firstName": "tet", "lastName": "test", "email": "test@email.com", "country": "Brasil", "currentIndustry": "aaaaaaaaaaaaa", "otherIndustry": "aaaaaaaaaaaaa", "currentOrganization": "test", "salary": "1234567" } delete myjsonobj['otherIndustry']; console.log(myjsonobj);
If you want to remove a key
when you know the value you can use Object.keys
function which returns an array of a given object's own enumerable properties.如果您想在知道值时删除
key
,您可以使用Object.keys
函数,该函数返回给定对象自己的可枚举属性的数组。
let value="test"; let myjsonobj = { "employeeid": "160915848", "firstName": "tet", "lastName": "test", "email": "test@email.com", "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);
There are several ways to do this, lets see them one by one:有几种方法可以做到这一点,让我们一一看:
const myObject = { "employeeid": "160915848", "firstName": "tet", "lastName": "test", "email": "test@email.com", "country": "Brasil", "currentIndustry": "aaaaaaaaaaaaa", "otherIndustry": "aaaaaaaaaaaaa", "currentOrganization": "test", "salary": "1234567" }; delete myObject['currentIndustry']; // OR delete myObject.currentIndustry; console.log(myObject);
let myObject = { "employeeid": "160915848", "firstName": "tet", "lastName": "test", "email": "test@email.com", "country": "Brasil", "currentIndustry": "aaaaaaaaaaaaa", "otherIndustry": "aaaaaaaaaaaaa", "currentOrganization": "test", "salary": "1234567" }; myObject.currentIndustry = undefined; myObject = JSON.parse(JSON.stringify(myObject)); console.log(myObject);
const myObject = { "employeeid": "160915848", "firstName": "tet", "lastName": "test", "email": "test@email.com", "country": "Brasil", "currentIndustry": "aaaaaaaaaaaaa", "otherIndustry": "aaaaaaaaaaaaa", "currentOrganization": "test", "salary": "1234567" }; const {currentIndustry, ...filteredObject} = myObject; console.log(filteredObject);
Or if you can use omit() of underscore js library:或者,如果您可以使用下划线js 库的omit() :
const filteredObject = _.omit(currentIndustry, 'myObject');
console.log(filteredObject);
When to use what??什么时候用什么??
If you don't wanna create a new filtered object, simply go for either option 1 or 2. Make sure you define your object with let while going with the second option as we are overriding the values.如果您不想创建新的过滤对象,只需选择选项 1 或 2。确保在使用第二个选项时使用let定义对象,因为我们将覆盖值。 Or else you can use any of them.
否则,您可以使用其中任何一个。
hope this helps :)希望这有帮助:)
Follow this, it can be like what you are looking:按照这个,它可以像你正在寻找的那样:
var obj = { Objone: 'one', Objtwo: 'two' }; var key = "Objone"; delete obj[key]; console.log(obj); // prints { "objtwo": two}
Here is one more example.这里还有一个例子。 (check the reference )
(检查参考)
const myObject = { "employeeid": "160915848", "firstName": "tet", "lastName": "test", "email": "test@email.com", "country": "Brasil", "currentIndustry": "aaaaaaaaaaaaa", "otherIndustry": "aaaaaaaaaaaaa", "currentOrganization": "test", "salary": "1234567" }; const {otherIndustry, ...otherIndustry2} = myObject; console.log(otherIndustry2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I had issues with trying to delete a returned JSON object and found that it was actually a string.我在尝试删除返回的 JSON 对象时遇到问题,发现它实际上是一个字符串。 If you JSON.parse() before deleting you can be sure your key will get deleted.
如果您在删除之前 JSON.parse() ,您可以确保您的密钥将被删除。
let obj;
console.log(this.getBody()); // {"AED":3.6729,"AZN":1.69805,"BRL":4.0851}
obj = this.getBody();
delete obj["BRL"];
console.log(obj) // {"AED":3.6729,"AZN":1.69805,"BRL":4.0851}
obj = JSON.parse(this.getBody());
delete obj["BRL"];
console.log(obj) // {"AED":3.6729,"AZN":1.69805}
function omit(obj, key) {
const {[key]:ignore, ...rest} = obj;
return rest;
}
You can use ES6 spread operators like this.您可以像这样使用 ES6 扩展运算符。 And to remove your key simply call
并删除您的密钥只需调用
const newJson = omit(myjsonobj, "otherIndustry");
Its always better if you maintain pure function when you deal with type=object
in javascript.如果在 JavaScript 中处理
type=object
时保持纯函数,它总是更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.