Keeping only certain properties in a JavaScript object
Just re-initialise the object:
myObj = {
p1: myObj.p1,
p2: myObj.p2,
p100: myObj.p100
};
Another way is to delete certain properties, which is less effective:
var prop = ['p1', 'p2', 'p100'];
for (var k in myObj) {
if (prop.indexOf(k) < 0) {
delete myObj[k];
}
}
This was the first hit when googling 'js keep only certain keys' so might be worth an update.
The most 'elegant' solution might just be using underscore.js
_.pick(myObj, 'p1', 'p2', 'p100')