close

how to remove json object key and value.?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn how to remove json object key and value.? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

how to remove json object key and value.?

  1. how to remove json object key and value.?

    to remove json object key and value 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");

  2. how to remove json object key and value.?

    to remove json object key and value 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");

Method 1

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}

Method 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.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment