考虑到一个对象:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
如何删除财产 regex 以完成下一个 myObject?
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};
考虑到一个对象:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
如何删除财产 regex 以完成下一个 myObject?
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};
当前回答
删除对象的两种方式
使用为... 在函数中删除用户(键) { const newUsers = {}; for (const uid in users) { if (uid!== key) { newUsers[uid] = users[uid]; } return newUsers }
或
delete users[key]
其他回答
正确的路
到2023年,你只能在一个线上做到这一点:
delete myObject.regex;
或
delete myObject["regex"];
(例如,如果你有一个对象obj与一个属性称为关键一,你不能做obj.key一,所以这里是obj(“关键一”)。
您在查询标题中使用的术语,从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()
一个没有财产的物体的克隆:
例如:
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 }
如果你不想修改原始对象。
移除物品,而无需转换物品
如果移动性是一种担忧,你可以通过从旧的所有属性复制创建一个全新的对象,除了你想要删除的所有属性。
让 myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” };让 prop = “regex”; const updatedObject = Object.keys(myObject).reduce((object, key) => { if (key!== prop) { object[key] = myObject[key] } return object }, {}) console.log(updatedObject);
在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"