I’m trying to remove properties from an object if its name has a length greater than 1, using .map():
var result = { a :{}, b : {}, invalid : {} };
result = $.map(result, function(val, prop){
return prop.length > 1 ? null : { prop : val };
});
console.log(result);
Console shows that result is now an array (not an object as it should be) and prop is taken literally (i.e. there are many properties named “prop” inside the array).
Any help would be great!
EDIT: solution combining .map() and delete:
$.map(result, function(val, prop){
if(prop.length > 1) delete result[prop]; // val is unused but required
});
I think
$.mapis ill-suited for this task. I would fall back on afor...inloop anddeleteproperties you don’t want:Example: http://jsfiddle.net/LHFdp/
If you really want to use
$.map, you can access properties dynamically using[](See this useful article on MDN for more information about working with objects):Example: http://jsfiddle.net/qrwjU/