考虑到一个对象:

let myObject = {
  "ircEvent": "PRIVMSG",
  "method": "newURI",
  "regex": "^http://.*"
};

如何删除财产 regex 以完成下一个 myObject?

let myObject = {
  "ircEvent": "PRIVMSG",
  "method": "newURI"
};

当前回答

尝试下面的方法. 将对象属性值分为未定义。

var myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; myObject.regex = undefined; myObject = JSON.parse(JSON.stringify(myObject)); console.log(myObject);

其他回答

一个没有财产的物体的克隆:

例如:

let object = { a: 1, b: 2, c: 3 };

我们必须删除A。

有一个明确的 prop 密钥: const { a,...rest } = 对象; 对象 = 休息; 有一个变量的 prop 密钥: const propKey = 'a'; const { [propKey]: propValue,...rest } = 对象; 对象 = 休息; A cool arrow 函数 <unk>: const removeProperty = (propKey, { [propKey]: propValue,...rest }) => 休息; 对象 = removeProperty('a', 对象); 多种属性 const removeProperties = (object,...keys)

使用

object = removeProperties(object, 'a', 'b') // result => { c: 3 }

const propsToRemove = ['a', 'b']
object = removeProperties(object, ...propsToRemove) // result => { c: 3 }

假设你有一个类似于此的对象:

var Hogwarts = {
    staff : [
        'Argus Filch',
        'Filius Flitwick',
        'Gilderoy Lockhart',
        'Minerva McGonagall',
        'Poppy Pomfrey',
        ...
    ],
    students : [
        'Hannah Abbott',
        'Katie Bell',
        'Susan Bones',
        'Terry Boot',
        'Lavender Brown',
        ...
    ]
};

删除物品财产

delete Hogwarts.staff;

delete Hogwarts['staff'];

删除 array index

现在,如果你想移除一个员工或学生,程序有点不同,因为两种属性都是由自己组成的。

Hogwarts.staff.splice(3, 1);

Hogwarts.staff.splice(Hogwarts.staff.indexOf('Minerva McGonnagall') - 1, 1);

笔记

虽然你技术上可以使用删除一个序列,使用它会导致收到错误的结果,当打电话,例如,Hogwarts.staff.length 后来。

因此,在从对象中删除值时,首先要考虑您是否正在处理对象属性,或者您是否正在处理序列值,并根据此选择适当的策略。

如果你想尝试这个,你可以使用这个Fiddle作为一个起点。

这里有很多好答案,但我只是想说,当使用删除删除在JavaScript中删除一个属性时,很常见首先检查该属性是否存在,以防止错误。

E.G

var obj = {"property":"value", "property2":"value"};

if (obj && obj.hasOwnProperty("property2")) {
  delete obj.property2;
} else {
  //error handling
}

由于JavaScript的动态性质,经常有情况,你只是不知道是否存在的财产。 检查obj是否存在之前&& 也确保你不会扔错误,因为在未定义的对象上呼叫 hasOwnProperty() 函数。

对不起,如果这并没有添加到您的具体使用案例,但我认为这是一个很好的设计,在管理对象和其属性时适应。

下面是ES6如何轻松地删除输入:

让 myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; const removeItem = “regex”; const { [removeItem]: remove,...rest } = myObject; console.log(remove); // “^http://.*” console.log(rest); // Object { ircEvent: “PRIVMSG”, 方法: “newURI” }

ECMAScript 2015(或ES6)配备了内置的反射对象,可以通过使用目标对象和属性密钥的 Reflect.deleteProperty() 函数来删除对象属性:

Reflect.deleteProperty(myJSONObject, 'regex');

相当于:

delete myJSONObject['regex'];

但是,如果对象的属性无法配置,则不能通过删除属性函数或删除操作员删除它:

let obj = Object.freeze({ prop: "value" });
let success = Reflect.deleteProperty(obj, "prop");
console.log(success); // false
console.log(obj.prop); // value

Object.freeze() 使对象的所有属性不可配置(除其他事物外)。 deleteProperty 函数(以及 delete 运营商)在试图删除其任何属性时返回虚假。

删除和删除的区别在于使用严格模式时:

"use strict";

let obj = Object.freeze({ prop: "value" });
Reflect.deleteProperty(obj, "prop"); // false
delete obj["prop"];
// TypeError: property "prop" is non-configurable and can't be deleted