我编写了以下代码来从对象“弹出”一个属性,就像它是一个数组一样。这看起来像是会让我被更严肃的程序员打耳光的那种代码,所以我想知道这样做的正确方法是什么:
// wrong way to pop:
for( key in profiles ){
var profile = profiles[key]; // get first property
profiles[key] = 0; // Save over property just in case "delete" actually deletes the property contents instead of just removing it from the object
delete profiles[key]; // remove the property from the object
break; // "break" because this is a loop
}
我应该在上面提到过,与真正的“pop”不同,我不需要以任何特定的顺序出现这些对象。我只需要取出一个并将其从父对象中删除。
for( key in profiles ){
你应该真正宣布key
为var
。
profiles[key] = 0; // Save over property just in case "delete" actually deletes the property contents instead of just removing it from the object
没必要。删除不会触及属性的值(或者在具有setter但没有getter的属性的情况下,甚至要求它具有值)。
如果对象在其原型上有任何可枚举的属性,那么这将做一些奇怪的事情。考虑
Object.prototype.foo = 42;
function pop(obj) {
for (var key in obj) {
// Uncomment below to fix prototype problem.
// if (!Object.hasOwnProperty.call(obj, key)) continue;
var result = obj[key];
// If the property can't be deleted fail with an error.
if (!delete obj[key]) { throw new Error(); }
return result;
}
}
var o = {};
alert(pop(o)); // alerts 42
alert(pop(o)); // still alerts 42
对象中的属性不存储在堆栈中,因此基本概念将无法可靠地工作(除了上述注释中提到的其他问题)。
如果你真的需要这样的构造尝试这样的事情。
var ObjectStack = function(obj) {
this.object = obj;
this.stack=[];
};
ObjectStack.prototype.push = function(key,value) {
this.object[key]=value;
this.stack.push(key);
};
ObjectStack.prototype.pop = function() {
var key = this.stack.pop();
var prop = this.object[key];
delete this.object[key];
return prop;
};
var my_obj = {};
var my_stack = new ObjectStack(my_obj);
my_stack.push("prop1",val1);
my_stack.push("prop2",val2);
var last_prop = my_stack.pop(); //val2
但是:ぁzxswい
浏览器中没有为http://jsfiddle.net/a8Rf6/5/循环提供的“正确”顺序。有些按照它们的顺序进行,其他的则首先进行数值索引。因此,如果不创建自己的自定义对象,就没有办法做到这一点
现在你可以简单地使用spread操作符和Rest方式: const {key,... profilesWithoutKey} =个人资料;
感谢for in
一种更好的方法,而不是直接修改输入数组。例如。
Object.defineProperty(Object.prototype, 'pop',{
writable: false
, configurable: false
, enumerable: false
, value: function (name) {
var value = this[name];
delete this[name];
return value;
}
});
在研究了上述所有评论和解决方案之后,我可以提供基于它们的完全就绪的解决方案:
let arr = [{label: "a"}, {label: "b"}, {label: "p"}, {label: "c"}]
let newArr = arr.filter(p => { return p.label !== "p";});
也许有人会派上用场; )
PS。如果您使用我建议的代码和jQuery库,您可能会在控制台中遇到错误。在这种情况下,下面给出的选项是合适的:
Object.prototype.pop = function() {
for (var key in this) {
if (!Object.hasOwnProperty.call(this, key)) continue;
var result = this[key];
if (!delete this[key]) throw new Error();
return result;
}
};
var obj = {
a: '1',
b: '2',
c: '3'
};
console.log(obj.pop()); // 1
console.log(obj.pop()); // 2
console.log(obj.pop()); // 3
console.log(obj); // Object { }