Possible Duplicate:
Remove item from object
I have an object like below:
questions = new Object();
questions = {
"q1":"answer1",
"q2":"answer2",
"q3":"answer3",
"q4":"answer4"
}
How do remove q3 and q4 and still preserve q1 and q1? Please suggest as dynamic approach to this as you can because questions object is populated dynamically and could have different number of items.
I tried using .slice with no luck:
question = "q2";
questions = questions.slice(0, question);
{}syntax actually creates an object, so thequestions = new Object();line is completely redundant. It's redundant anyway because you are overwriting it.delete objectHowever, this doesn't deliver what I'd like to achieve. Basically, I want to remove remaining items from a certain position (or index).Object.keys, sort the Array of keys, then iterate the Array from the desired point forward, removing each property of the object one at a time usingdelete.