我编写了以下代码来从对象"弹出"一个属性,就像它是一个数组一样.这看起来像是会让我被更严肃的程序员打耳光的那种代码,所以我想知道这样做的正确方法是什么:
// 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"不同,我不需要以任何特定的顺序出现这些对象.我只需要取出一个并将其从父对象中删除.