考虑到一个对象:
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"
};
当前回答
另一个替代方案是使用 Underscore.js 图书馆。
請注意, _.pick() 和 _.omit() 兩者都返回對象的副本,並且不直接修改原始對象。
参考: 链接 _.pick(对象, *keys)
返回对象的副本,过滤到只有值为白化密钥(或一系列有效的密钥)。
var myJSONObject =
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
_.pick(myJSONObject, "ircEvent", "method");
=> {"ircEvent": "PRIVMSG", "method": "newURI"};
参考: 链接 _.omit(对象, * 关键)
返回对象的副本,过滤以忽略黑名单的钥匙(或一系列的钥匙)。
var myJSONObject =
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
_.omit(myJSONObject, "regex");
=> {"ircEvent": "PRIVMSG", "method": "newURI"};
对于序列,可以类似的方式使用 _.filter() 和 _.reject()。
其他回答
您可以使用如下的过滤器。
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)
var myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; 删除 myObject.regex; console.log ( myObject.regex); // logs: undefined
它在Firefox和Internet Explorer工作,我认为它在其他所有工作。
您在查询标题中使用的术语,从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()
这个帖子很古老,我觉得它非常有用,所以我决定分享我写的 unset 函数,如果其他人看到这个帖子,想想为什么它不那么简单,因为它在 PHP unset 函数。
写下这个新的 unset 函数的原因,是保持所有其他变量的指数在这个 hash_map. 查看下面的例子,看看如何在从 hash_map 中删除值后没有改变“test2”的指数。
function unset(unsetKey, unsetArr, resort) { var tempArr = unsetArr; var unsetArr = {}; delete tempArr[unsetKey]; if (resort) { j = -1; } for (i in tempArr) { if (typeof(tempArr[i]) !== 'undefined') { if (resort) { j++; } else { j = i; } unsetArr[j] = tempArr[i]; } } return unsetArr; } var unsetArr = ['test', 'deletedString', 'test2']; console.log(unset('1', unsetArr, true)); // output Object {0: "test", 1: "test2"} console.log(unset('1', unsetArr, false)); // output Object {0: "test", 2: "test2"}
如果您想删除对物体深沉的财产,那么您可以使用下列重复功能,路径到财产作为第二个论点:
var deepObjectRemove = function(obj, path_to_key){
if(path_to_key.length === 1){
delete obj[path_to_key[0]];
return true;
}else{
if(obj[path_to_key[0]])
return deepObjectRemove(obj[path_to_key[0]], path_to_key.slice(1));
else
return false;
}
};
例子:
var a = {
level1:{
level2:{
level3: {
level4: "yolo"
}
}
}
};
deepObjectRemove(a, ["level1", "level2", "level3"]);
console.log(a);
//Prints {level1: {level2: {}}}