考虑到一个对象:

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

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

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

当前回答

您在查询标题中使用的术语,从JavaScript对象中删除一个属性,可以以不同的方式解释。 一个是将其删除整个内存和对象密钥列表或另一个只是将其从您的对象中删除。 正如在其他答案中提到的那样,删除关键词是主要部分。

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

如果你做:

console.log(Object.keys(myJSONObject));

结果将是:

["ircEvent", "method", "regex"]

delete myJSONObject["regex"];

["ircEvent", "method"]

但问题是,如果你关心记忆,你想完成的对象从记忆中被删除,建议在删除密钥之前将其设置为零:

myJSONObject["regex"] = null;
delete myJSONObject["regex"];

var regex = myJSONObject["regex"];

或者将其作为一个新的指标添加到另一个对象,如:

var myOtherObject = {};
myOtherObject["regex"] = myJSONObject["regex"];

delete regex; //False

结果将是虚假的,这意味着您的删除声明没有按照您的预期进行。但是,如果您以前没有创建这个变量,并且您只有 myOtherObject(“regex”)作为您的最后一个现有参考,您可能只是通过删除它:

myOtherObject["regex"] = null;
delete myOtherObject["regex"];

换句话说,一个JavaScript对象可以被杀,一旦在您的指向该对象的代码中没有留下任何参考。


更新:

谢谢 @AgentME:

更多关于 Object.seal 的信息: Object.seal()

其他回答

您可以使用 Delete Property 删除对象中的财产[键]

在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 6功能,它是如此简单:

const { a, ...rest } = { a: 1, b: 2, c: 3 };

或与问题样本:

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

你可以在Babel try-out编辑中看到它。


编辑:

要重新分配到相同的变量,请使用:

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

短答

var obj = {
  data: 1,
  anotherData: 'sample'    
}
delete obj.data //this removes data from the obj

你被留在

var obj = {
  anotherData: 'sample'    
}

您可以使用如下的过滤器。

var myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; // Way 1 let filter1 = {} Object.keys({...myObject}).filter(d => { if(d!=='regex'){ filter1[d] = myObject[d]; }) console.log(filter1) // Way 2 let filter2 = Object.fromEntries(Object.entries({...myObject}).filter(d => d[0]!=='regex' )) console.log(filter2)