Javascript 如何删除json对象键和值。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46599519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:30:48  来源:igfitidea点击:

how to remove json object key and value.?

javascriptjqueryjsonobject

提问by krish kr

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.

我有一个 json 对象,如下所示。我想通过使用以下不起作用的代码删除“otherIndustry”条目及其值。

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.

如何删除 Json 对象特定的键及其值。下面是我的示例 json 对象,我想在其中删除“otherIndustry”键及其值。

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.

日志仍然打印相同的对象而不从对象中删除“otherIndustry”条目。

回答by Mihai Alexandru-Ionut

deleteoperator is used to removean object property.

delete运算符用于remove对象property

deleteoperator does notreturns the new object, only returns a boolean: trueor false.

delete运算符返回新对象,只返回一个boolean: truefalse

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

另一方面,解释器执行后var updatedjsonobj = delete myjsonobj['otherIndustry'];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": "[email protected]",
  "country": "Brasil",
  "currentIndustry": "aaaaaaaaaaaaa",
  "otherIndustry": "aaaaaaaaaaaaa",
  "currentOrganization": "test",
  "salary": "1234567"
}
delete myjsonobj['otherIndustry'];
console.log(myjsonobj);

If you want to remove a keywhen you know the value you can use Object.keysfunction which returns an array of a given object's own enumerable properties.

如果您想key在知道值时删除 a ,您可以使用Object.keys返回给定对象自己的可枚举属性数组的函数。

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

回答by Mohammed Amir Ansari

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

有几种方法可以做到这一点,让我们一一看:

  1. deletemethod: The most common way
  1. 删除方法:最常用的方法

const myObject = {
    "employeeid": "160915848",
    "firstName": "tet",
    "lastName": "test",
    "email": "[email protected]",
    "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:
  1. 通过使键值未定义:替代和更快的方法:

let myObject = {
    "employeeid": "160915848",
    "firstName": "tet",
    "lastName": "test",
    "email": "[email protected]",
    "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 spreadOperator:
  1. 使用 es6扩展运算符:

const myObject = {
    "employeeid": "160915848",
    "firstName": "tet",
    "lastName": "test",
    "email": "[email protected]",
    "country": "Brasil",
    "currentIndustry": "aaaaaaaaaaaaa",
    "otherIndustry": "aaaaaaaaaaaaa",
    "currentOrganization": "test",
    "salary": "1234567"
};


const {currentIndustry, ...filteredObject} = myObject;
console.log(filteredObject);

Or if you can use omit()of underscorejs library:

或者,如果你可以用略()下划线JS库:

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 letwhile going with the second option as we are overriding the values. Or else you can use any of them.

如果您不想创建新的过滤对象,只需选择选项 1 或 2。确保在使用第二个选项时使用let定义对象,因为我们将覆盖值。否则,您可以使用其中任何一个。

hope this helps :)

希望这可以帮助 :)

回答by Muayyad Ayesh

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}

回答by Penny Liu

Here is one more example. (check the reference)

这里还有一个例子。(检查参考

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);
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

回答by Nick Gooch

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.

我在尝试删除返回的 JSON 对象时遇到问题,发现它实际上是一个字符串。如果您在删除之前 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}