考虑到一个对象:

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

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

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

当前回答

有几种方法可以从物体中删除属性:

1) 使用点属性附件移除(可变)

2. 使用方形布拉克特属性配件移除(可转换)

const myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*”, }; delete myObject[‘regex’]; console.log(myObject); // or const name = ‘ircEvent’; delete myObject[name]; console.log(myObject);

3) 替代选项,但没有改变原始对象,使用对象破坏和休息合成(不变)

const myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*”, }; const { regex,...myObjectRest} = myObject; console.log(myObjectRest);

其他回答

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

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

要从对象中删除一个属性(指向对象),你可以这样做:

delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];

Demo var myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; 删除 myObject.regex; console.log(myObject);

对于任何有兴趣阅读更多关于它的人来说,Stack Overflow 用户 Kangax 写了一篇关于他们博客的删除声明的令人难以置信的深入博客帖子,理解删除。

如果你想要一个新的对象,除了一些,所有原始的钥匙,你可以使用破坏。

@johnstock,我们也可以使用JavaScript的原型化概念,将方法添加到对象中,以删除在呼叫对象中可用的任何过渡密钥。

上面的答案值得欣赏。

var myObject = {“ircEvent”:“PRIVMSG”,“方法”:“newURI”,“regex”:“^http://.*” }; // 1st 和直接方式删除 myObject.regex; // 删除 myObject[“regex”] console.log(myObject); // { ircEvent:‘PRIVMSG’,方法:‘newURI’ } // 2 方式 - 使用 JavaScript 的原型概念 Object.prototype.removeFromObjectByKey = 函数(关键) { // 如果有,删除它并删除它

在JavaScript中删除财产

突变物品属性删除,不安全

此类别适用于使用对象字母或对象例子时,您希望保持/继续使用原始参考,并且在您的代码中不使用无标志的功能原则。

'use strict'
const iLikeMutatingStuffDontI = { myNameIs: 'KIDDDDD!', [Symbol.for('amICool')]: true }
delete iLikeMutatingStuffDontI[Symbol.for('amICool')] // true
Object.defineProperty({ myNameIs: 'KIDDDDD!', 'amICool', { value: true, configurable: false })
delete iLikeMutatingStuffDontI['amICool'] // throws

基于回归的链条财产遗弃

此类别适用于在更新的 ECMAScript 品味中运行平面对象或序列示例,当不变化的方法是需要的,并且您不需要为符号密钥负责:

const foo = { name: 'KIDDDDD!', [Symbol.for('isCool')]: true }
const { name, ...coolio } = foo // coolio doesn't have "name"
const { isCool, ...coolio2 } = foo // coolio2 has everything from `foo` because `isCool` doesn't account for Symbols :(

突变物品属性删除,安全

'use strict'
const iLikeMutatingStuffDontI = { myNameIs: 'KIDDDDD!', [Symbol.for('amICool')]: true }
Reflect.deleteProperty(iLikeMutatingStuffDontI, Symbol.for('amICool')) // true
Object.defineProperty({ myNameIs: 'KIDDDDD!', 'amICool', { value: true, configurable: false })
Reflect.deleteProperty(iLikeMutatingStuffDontI, 'amICool') // false

const foo = { name: 'KIDDDDD!', [Symbol.for('isCool')]: true }
const { name, ...coolio } = foo // coolio doesn't have "name"
const { isCool, ...coolio2 } = foo // coolio2 has everything from `foo` because `isCool` doesn't account for Symbols :(

基于图书馆的财产遗弃

这个类别通常允许更大的功能灵活性,包括对符号的会计 & 在一个声明中忽略多个属性:

const o = require("lodash.omit")
const foo = { [Symbol.for('a')]: 'abc', b: 'b', c: 'c' }
const bar = o(foo, 'a') // "'a' undefined"
const baz = o(foo, [ Symbol.for('a'), 'b' ]) // Symbol supported, more than one prop at a time, "Symbol.for('a') undefined"

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