-3

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);
Community
  • 1
  • 1
Still Questioning
  • 670
  • 2
  • 7
  • 23
  • 3
    Note that the `{}` syntax actually creates an object, so the `questions = new Object();` line is completely redundant. It's redundant anyway because you are overwriting it. – Esailija Jul 26 '12 at 19:38
  • Thanks. I'm familiar with `delete object` However, this doesn't deliver what I'd like to achieve. Basically, I want to remove remaining items from a certain position (or index). – Still Questioning Jul 26 '12 at 19:47
  • 1
    @StillQuestioning: Object properties have no position. –  Jul 26 '12 at 19:47
  • 1
    @StillQuestioning object keys are inherently unordered in javascript. They will never have an index or position. Perhaps you want an array instead? – Alex Wayne Jul 26 '12 at 19:49
  • 1
    @StillQuestioning: If the keys are such that they're sortable, closest you could come would be to get the keys with `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 using `delete`. –  Jul 26 '12 at 19:49
  • ahh..i see.. so there's never a possibility to automatically refer to the certain set of items unless i loop through them and match with conditions? – Still Questioning Jul 26 '12 at 19:54
  • @StillQuestioning: Correct. Sorting helps a little if possible, like your `q1`, `q2`, etc could be sorted, which would then allow you to remove all above a certain point. But if that's not the nature of your keys, then yes, it would be a matter of enumerating all the properties, and removing those that meet a condition. There's no guarantee that the order of enumeration will be consistent. –  Jul 26 '12 at 21:10
  • @amnotiam @Alex Following you guys' suggestion of making us of the `Object.key()`, i got it working on all major browser except for ie8. Looks like ie8 doesn't support `Object.key()` Any best practice tweaks and tricks to this that you know of? thanks!!! – Still Questioning Jul 27 '12 at 20:58
  • I got this:`if(typeof Object.keys !=="function"){ Object.keys = function(obj){ var arr = []; for (var k in obj) { if (obj.hasOwnProperty(k)) arr.push(k); } return arr; } }` – Still Questioning Jul 27 '12 at 21:08
  • @StillQuestioning: Yep, that'll work as a shim for `Object.keys`. Can't think of any worthwhile tweaks off hand. –  Jul 27 '12 at 21:13
  • Simply use delete like this`delete questions.q2` or `delete questions[question]` – rotimi-best Feb 18 '19 at 11:04

1 Answers1

5
delete questions.q1;
console.log(questions.q1); // undefined

The delete keyword removes properties from objects. It's different form setting a property to null, it removes the key entirely.

var a = { b: 123 };
console.log(a.b);             // 123
console.log(Object.keys(a));  // ['b']

a.b = null;
console.log(a.b);             // null
console.log(Object.keys(a));  // ['b']

a.b = void 0;
console.log(a.b);             // undefined
console.log(Object.keys(a));  // ['b']

delete a.b;
console.log(a.b);             // undefined
console.log(Object.keys(a));  // []
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • If I do `a.b = void 0;` it wont erase the key. `Object.keys(a)` will still return `['b']`. The value of the key is simply `undefined`. It doesn't change much at all. – Alex Wayne Jul 26 '12 at 20:03
  • Yeah my bad, I didn't notice them (was only judging the logs). Deleted. – Esailija Jul 26 '12 at 20:10