Chr*_*row 19 javascript iterator object
我编写了以下代码来从对象"弹出"一个属性,就像它是一个数组一样.这看起来像是会让我被更严肃的程序员打耳光的那种代码,所以我想知道这样做的正确方法是什么:
// 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
}
Run Code Online (Sandbox Code Playgroud)
我应该在上面提到过,与真正的"pop"不同,我不需要以任何特定的顺序出现这些对象.我只需要取出一个并将其从父对象中删除.
Noa*_*Gal 29
现在,您可以简单地使用扩展运算符及其 Rest 方式:
const { key, ...profilesWithoutKey } = profiles;
Run Code Online (Sandbox Code Playgroud)
归功于这篇博文
Mik*_*uel 13
for( key in profiles ){
Run Code Online (Sandbox Code Playgroud)
你应该真的宣布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
Run Code Online (Sandbox Code Playgroud)
没必要.删除不会触及属性的值(或者在具有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
Run Code Online (Sandbox Code Playgroud)
对象中的属性不存储在堆栈中,因此基本概念无法可靠地工作(除了上面评论中提到的其他问题)。
如果您真的需要这样的构造,请尝试这样的操作。
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
Run Code Online (Sandbox Code Playgroud)
演示:http : //jsfiddle.net/a8Rf6/5/
| 归档时间: |
|
| 查看次数: |
43408 次 |
| 最近记录: |