如何删除json对象键和值.

kri*_* kr 21 javascript jquery json object

我有一个json对象,如下所示.我想删除"otherIndustry"条目及其值,使用下面的代码不起作用.

var updatedjsonobj = delete myjsonobj['otherIndustry'];
Run Code Online (Sandbox Code Playgroud)

如何删除Json对象特定的键及其值.下面是我的示例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);
Run Code Online (Sandbox Code Playgroud)

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

Mih*_*nut 29

delete运算符用于remove对象property.

deleteoperator 返回新对象,只返回boolean: truefalse.

另一方面,在解释器执行之后var updatedjsonobj = delete myjsonobj['otherIndustry'];,updatedjsonobj变量将存储一个boolean 值.

如何删除Json对象特定的键及其值?

您只需要知道属性名称,以便从对象的属性中删除它.

delete myjsonobj['otherIndustry'];
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

如果要key在知道值时删除a ,可以使用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);
Run Code Online (Sandbox Code Playgroud)


Moh*_*ari 9

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

  1. 删除方法:最常用的方法

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);
Run Code Online (Sandbox Code Playgroud)

  1. 通过使键值未定义:替代和更快的方法:

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);
Run Code Online (Sandbox Code Playgroud)

  1. 使用 es6扩展运算符:

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);
Run Code Online (Sandbox Code Playgroud)

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

const filteredObject = _.omit(currentIndustry, 'myObject');
console.log(filteredObject);
Run Code Online (Sandbox Code Playgroud)

什么时候用什么??

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

希望这可以帮助 :)


小智 5

按照这个,它可以像你正在寻找的那样:

var obj = {
    Objone: 'one',
    Objtwo: 'two'
};

var key = "Objone";
delete obj[key];
console.log(obj); // prints { "objtwo": two}
Run Code Online (Sandbox Code Playgroud)