jquery - Remove certain elements from map in Javascript -
how can remove key/value pairs following map, key starts x.
var map = new object(); map[xkey1] = "value1"; map[xkey2] = "value2"; map[ykey3] = "value3"; map[ykey4] = "value4";
edit
is there way through regular expression, using ^ . map[^xke], key starts 'xke' instead of 'x'
you can iterate on map keys using object.key
.
the simple solution :
demo here
object.keys(map).foreach(function (key) { if(key.match('^'+letter)) delete obj[key]; });
so here other version of removekeystartswith
regular expression said:
function removekeystartswith(obj, letter) { object.keys(obj).foreach(function (key) { //if(key[0]==letter) delete obj[key];////without regex if(key.match('^'+letter)) delete obj[key];//with regex }); } var map = new object(); map['xkey1'] = "value1"; map['xkey2'] = "value2"; map['ykey3'] = "value3"; map['ykey4'] = "value4"; console.log(map); removekeystartswith(map, 'x'); console.log(map);
solution regex
cover need if use letter=xke said other solution without regex , need replace :
key[0]==letter
key.substr(0,3)==letter
Comments
Post a Comment