In JS;
Trying to write a function that takes in an array of objects and a series of arguments. The function will remove any properties not given as arguments.
example: input cleanseData([{a: 'b', c:'d'}, {a: 'q'}], 'a');
output [{a: 'b'}, {a: 'q'}]
Here is the function that tried but the objects remain unchanged.
var cleanseData = function(listObj, key1, key2, key3) {
for (var i=0; i<listObj.length; i++) {
for(k in listObj[i]) {
if(k !== key1 && k!==key2 && k!==key3) {
delete listObj[i].k;
}
}
}
return listObj;
}
if (keys.indexOf(k) == -1) {/* delete key */}. But you need a hasOwnProperty test to exclude inherited properties, or use Object.keys. You might also have issues with!==given that keys are always strings and you might provide numbers as arguments.