76

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": "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.

krish kr
  • 857
  • 1
  • 6
  • 7
  • Possible duplicate of [Remove key-value pair from JSON object](https://stackoverflow.com/questions/24770887/remove-key-value-pair-from-json-object) – Pankaj Makwana Oct 06 '17 at 06:27
  • Your code should work, can you create a MVCE https://stackoverflow.com/help/mcve – gurvinder372 Oct 06 '17 at 06:28
  • delete myObj.other.key1; [As shown in this example.](https://stackoverflow.com/questions/1219630/remove-a-json-attribute/1219633#1219633) – Najam Us Saqib Oct 06 '17 at 06:30
  • *which doesn't worked* What was the result? can you please explain? – Rajesh Oct 06 '17 at 06:31
  • Nope @Alexandru-IonutMihai, actually my code is `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 );` still prints the same object. :( – krish kr Oct 06 '17 at 07:03
  • @krishkr, and what are you trying to do ? – Mihai Alexandru-Ionut Oct 06 '17 at 07:04
  • @krishkr, prints `typeof myjsonobj` and tell me what you've received. – Mihai Alexandru-Ionut Oct 06 '17 at 07:07
  • @Alexandru-IonutMihai console.log(typeof myjsonobj); prints "string" :( how can i convert it to json before json delete method usage. ? – krish kr Oct 06 '17 at 07:13
  • @krishkr, use this: `myjsonobj=JSON.parse(myjsonobj); delete myjsonobj['otherIndustry']` – Mihai Alexandru-Ionut Oct 06 '17 at 07:15
  • @Alexandru-IonutMihai thanks a lot that works, can you help me how to get back to the string after json parse which is needed to my AJAX call. – krish kr Oct 06 '17 at 07:36
  • Yes, just use `myjsonobj=JSON.stringify(myjsonobj)` – Mihai Alexandru-Ionut Oct 06 '17 at 07:36
  • Possible duplicate of [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – pscl Dec 15 '17 at 06:59

6 Answers6

120

delete operator is used to remove an object property.

delete operator does not returns the new object, only returns a boolean: true or false.

In the other hand, after interpreter executes var updatedjsonobj = delete myjsonobj['otherIndustry']; , updatedjsonobj variable will store a boolean value.

How to remove Json object specific key and its value ?

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.

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);
Vince
  • 544
  • 5
  • 15
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
17

There are several ways to do this, lets see them one by one:

  1. delete method: The most common way

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);
  1. By making key value undefined: Alternate & a faster way:

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);
  1. With es6 spread Operator:

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:

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 :)

Mohammed Amir Ansari
  • 2,311
  • 2
  • 12
  • 26
8

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}
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
2

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;
}
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
2
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.

sachinkondana
  • 564
  • 5
  • 15
0

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}