考虑到一个对象:
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"
};
当前回答
讓 myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; obj = Object.fromEntries( Object.entries(myObject).filter(功能(m){ return m[0]!= “regex”/*或任何關鍵去刪除*/ } ) console.log(obj)
您也可以使用 Object.entries 处理对象类似于 a2d 序列,并使用 序列删除对象,如您在正常序列中一样,或者简单地通过对象过滤,如您在序列中一样,并将重建的对象归还原始变量。
其他回答
尝试下面的方法. 将对象属性值分为未定义。
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 obj = {
data: 1,
anotherData: 'sample'
}
delete obj.data //this removes data from the obj
你被留在
var obj = {
anotherData: 'sample'
}
正确的路
到2023年,你只能在一个线上做到这一点:
delete myObject.regex;
或
delete myObject["regex"];
(例如,如果你有一个对象obj与一个属性称为关键一,你不能做obj.key一,所以这里是obj(“关键一”)。
另一个解决方案,使用 Array#reduce。
(六)
const myObject = { ircEvent: 'PRIVMSG',方法: 'newURI', regex: '^http://.*', }; const myNewObject = Object.keys(myObject).reduce((obj, key) => { key!=='regex'? obj[key] = myObject[key] : null; return obj; }, {}); console.log(myNewObject);