如何删除json对象键和值。

时间:2017-10-06 06:25:06

标签: javascript jquery json object

我有一个json对象,如下所示。我想删除" otherIndustry"通过使用下面没有工作的代码输入及其价值。

var updatedjsonobj = delete myjsonobj['otherIndustry'];

如何删除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);

其中日志仍然打印相同的对象而不删除' otherIndustry'从对象进入。

6 个答案:

答案 0 :(得分:19)

delete运算符用于remove对象property

delete运算符返回新对象,只返回boolean true false

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

  

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

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

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

如果要在知道值时删除key,可以使用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);

答案 1 :(得分:3)

按照这个,它可能就像你在看的那样:

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

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

答案 2 :(得分:2)

这里是另一个示例。 (选中 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;
}

答案 3 :(得分:2)

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

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

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

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

或者如果您可以使用下划线 js库的 omit()

const filteredObject = _.omit(currentIndustry, 'myObject');
console.log(filteredObject);

何时使用什么?

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

希望这会有所帮助:)

答案 4 :(得分:0)

我在尝试删除返回的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}

答案 5 :(得分:0)

function omit(obj, key) {
    const {[key]:ignore, ...rest} = obj;
    return rest;
}

您可以像这样使用ES6传播运算符。而要删除密钥,只需致电

const newJson = omit(myjsonobj, "otherIndustry");

如果在javascript中处理type=object时保持纯函数,总会更好。