In this example, We have shared how to remove certain elements from map in Javascript. If you really want to use regular expressions (but why?), then the following works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function removeKeyStartsWith(obj, letter, caseSensitive) { var sensitive = caseSensitive === false ? 'i' : '', reg = new RegExp('^' + letter, sensitive); for (var prop in obj) { if (obj.hasOwnProperty(prop) && reg.test(prop)){ delete obj[prop]; } } } var map = new Object(); map['XKey1'] = "Value1"; map['XKey2'] = "Value2"; map['YKey3'] = "Value3"; map['YKey4'] = "Value4"; console.log(map); removeKeyStartsWith(map, 'x', true); console.log(map); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.