I have a json object as shown below. where i want to delete the "otherIndustry" entry and its value by using below code which doesn't worked.
var updatedjsonobj = delete myjsonobj['otherIndustry'];
How to remove Json object specific key and its value. Below is my example json object where i want to remove "otherIndustry" key and its value.
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);
where the log still prints the same object without removing 'otherIndustry' entry from the object.
This question is related to
javascript
jquery
json
object
function omit(obj, key) {
const {[key]:ignore, ...rest} = obj;
return rest;
}
You can use ES6 spread operators like this. 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.
I had issues with trying to delete a returned JSON object and found that it was actually a string. If you JSON.parse() before deleting you can be sure your key will get deleted.
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}
Here is one more example. (check the reference)
const myObject = {_x000D_
"employeeid": "160915848",_x000D_
"firstName": "tet",_x000D_
"lastName": "test",_x000D_
"email": "[email protected]",_x000D_
"country": "Brasil",_x000D_
"currentIndustry": "aaaaaaaaaaaaa",_x000D_
"otherIndustry": "aaaaaaaaaaaaa",_x000D_
"currentOrganization": "test",_x000D_
"salary": "1234567"_x000D_
};_x000D_
const {otherIndustry, ...otherIndustry2} = myObject;_x000D_
console.log(otherIndustry2);
_x000D_
.as-console-wrapper {_x000D_
max-height: 100% !important;_x000D_
top: 0;_x000D_
}
_x000D_
Follow this, it can be like what you are looking:
var obj = {_x000D_
Objone: 'one',_x000D_
Objtwo: 'two'_x000D_
};_x000D_
_x000D_
var key = "Objone";_x000D_
delete obj[key];_x000D_
console.log(obj); // prints { "objtwo": two}
_x000D_
There are several ways to do this, lets see them one by one:
const myObject = {_x000D_
"employeeid": "160915848",_x000D_
"firstName": "tet",_x000D_
"lastName": "test",_x000D_
"email": "[email protected]",_x000D_
"country": "Brasil",_x000D_
"currentIndustry": "aaaaaaaaaaaaa",_x000D_
"otherIndustry": "aaaaaaaaaaaaa",_x000D_
"currentOrganization": "test",_x000D_
"salary": "1234567"_x000D_
};_x000D_
_x000D_
delete myObject['currentIndustry'];_x000D_
// OR delete myObject.currentIndustry;_x000D_
_x000D_
console.log(myObject);
_x000D_
let myObject = {_x000D_
"employeeid": "160915848",_x000D_
"firstName": "tet",_x000D_
"lastName": "test",_x000D_
"email": "[email protected]",_x000D_
"country": "Brasil",_x000D_
"currentIndustry": "aaaaaaaaaaaaa",_x000D_
"otherIndustry": "aaaaaaaaaaaaa",_x000D_
"currentOrganization": "test",_x000D_
"salary": "1234567"_x000D_
};_x000D_
_x000D_
myObject.currentIndustry = undefined;_x000D_
myObject = JSON.parse(JSON.stringify(myObject));_x000D_
_x000D_
console.log(myObject);
_x000D_
const myObject = {_x000D_
"employeeid": "160915848",_x000D_
"firstName": "tet",_x000D_
"lastName": "test",_x000D_
"email": "[email protected]",_x000D_
"country": "Brasil",_x000D_
"currentIndustry": "aaaaaaaaaaaaa",_x000D_
"otherIndustry": "aaaaaaaaaaaaa",_x000D_
"currentOrganization": "test",_x000D_
"salary": "1234567"_x000D_
};_x000D_
_x000D_
_x000D_
const {currentIndustry, ...filteredObject} = myObject;_x000D_
console.log(filteredObject);
_x000D_
Or if you can use omit() of underscore js library:
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. Or else you can use any of them.
hope this helps :)
Source: Stackoverflow.com