如何删除json对象的键和值?

问题描述 投票:0回答:7

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

var updatedjsonobj = delete myjsonobj['otherIndustry'];

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

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

javascript jquery json object
7个回答
123
投票

delete
运算符用于
remove
对象
property

delete
运算符返回新对象,仅返回
boolean
truefalse

另一方面,解释器执行

var updatedjsonobj = delete myjsonobj['otherIndustry'];
后,
updatedjsonobj
变量将存储一个
boolean
值。

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

您只需要知道属性名称即可将其从对象的属性中删除。

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

如果您想在知道值时删除

key
,可以使用
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);


19
投票

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

  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. 通过使键值未定义:替代且更快的方法:

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. 使用 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);

或者如果你可以使用 underscore js 库的 omit():

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

什么时候用什么??

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

希望这有帮助:)


8
投票

按照这个,它可以像你所看到的那样:

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

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


2
投票

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

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


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

您可以像这样使用 ES6 扩展运算符。要删除您的钥匙,只需致电

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

在 javascript 中处理

type=object
时,如果保持纯函数总是更好。


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}

0
投票

如果您想重新分配一个值(或2个值),您可以使用@Mohammed Amir Ansari的答案并像这样修改它:

const myCarObject= { manufacturer: 'BMW',model: "M4", hp:503  };
const { model, hp, ...manufacturer} = myCarObject;
console.log(manufacturer)
//OUTPUT:
//{ manufacturer: 'BMW'}

console.log(model)
//OUTPUT:
//"M4"

console.log(hp)
//OUTPUT:
//503

这将从汽车对象中提取值并将它们分配给其他变量。

聚会有点晚了,希望对某人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.