Remove certain elements from map in Javascript
如何从下面的映射中删除所有键/值对,其中键以x开头。
1 2 3 4 5 | var map = new Object(); map[XKey1] ="Value1"; map[XKey2] ="Value2"; map[YKey3] ="Value3"; map[YKey4] ="Value4"; |
编辑
是否有任何方法可以通过正则表达式,可能使用^。类似于map[^xke],其中键以"xke"开头,而不是"x"
我建议:
1 2 3 4 5 6 7 | function removeKeyStartsWith(obj, letter) { for (var prop in obj) { if (obj.hasOwnProperty(prop) && prop[0] == letter){ delete obj[prop]; } } } |
JS小提琴演示。
顺便说一句,使用对象文本而不是构造函数通常更容易(并且似乎被认为是"更好的实践"),因此值得展示以下内容(即使出于某种原因,您更喜欢
1 2 3 4 5 6 | var map = { 'XKey1' :"Value1", 'XKey2' :"Value2", 'YKey3' :"Value3", 'YKey4' :"Value4", }; |
JS小提琴演示。
如果您真的想使用正则表达式(但为什么呢?),然后进行以下工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function removeKeyStartsWith(obj, letter, caseSensitive) { // case-sensitive matching: 'X' will not be equivalent to 'x', // case-insensitive matching: 'X' will be considered equivalent to 'x' var sensitive = caseSensitive === false ? 'i' : '', // creating a new Regular Expression object, // ^ indicates that the string must *start with* the following character: 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); |
JS小提琴演示。
最后(至少目前)一种扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Object.prototype.removeIf = function (needle, opts) { var self = this, settings = { 'beginsWith' : true, 'endsWith' : false, 'sensitive' : true }; opts = opts || {}; for (var p in settings) { if (settings.hasOwnProperty(p)) { settings[p] = typeof opts[p] == 'undefined' ? settings[p] : opts[p]; } } var modifiers = settings.sensitive === true ? '' : 'i', regString = (settings.beginsWith === true ? '^' : '') + needle + (settings.endsWith === true ? '$' : ''), reg = new RegExp(regString, modifiers); for (var prop in self) { if (self.hasOwnProperty(prop) && reg.test(prop)){ delete self[prop]; } } return self; }; var map = { 'XKey1' :"Value1", 'XKey2' :"Value2", 'YKey3' :"Value3", 'YKey4' :"Value4", }; console.log(map); map.removeIf('xkey2', { 'beginsWith' : true, 'endsWith' : true, 'sensitive' : false }); console.log(map); |
JS小提琴演示。
参考文献:
Object.hasOwnProperty() 。- javascript正则表达式。
您可以使用
1 2 3 | Object.keys(map).forEach(function (key) { if(key.match('^'+letter)) delete obj[key]; }); |
所以这里有另外一个版本的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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); |
使用
你可以很容易地用这种方法得到它。
1 2 3 4 5 6 7 8 | var map = new Object(); map['Key1'] ="Value1"; map['Key2'] ="Value2"; map['Key3'] ="Value3"; map['Key4'] ="Value4"; console.log(map); delete map["Key1"]; console.log(map); |
这是移除它的简单方法。
先决条件
假设您的原始输入:
1 2 3 4 5 6 | var map = new Object(); map[XKey1] ="Value1"; map[XKey2] ="Value2"; map[YKey3] ="Value3"; map[YKey4] ="Value4"; |
假设一个变量
1 2 3 4 5 6 7 | var clone = {}; $.each(map, function (k, v) { if (k.indexOf(pattern) == 0) { // k not starting with pattern clone[k] = v; } }); |
解决方案2-使用纯ecmascript并创建新对象
1 2 3 4 5 6 7 | var clone = {}; for (var k in map) { if (map.hasOwnProperty(k) && (k.indexOf(pattern) == 0)) { clone[k] = map[k]; } } |
解决方案3-使用纯ecmascript并使用源对象
1 2 3 4 5 | for (var k in map) { if (map.hasOwnProperty(k) && (k.indexOf(pattern) == 0)) { delete map[k]; } } |
或者,在现代浏览器中:
1 2 3 4 5 | Object.keys(map).forEach(function (k) { if (k.indexOf(pattern) == 0) { delete map[k]; } }); |
更新-使用正则表达式进行模式匹配
如果键
1 | k[0] == letter // to match or letter |
如果键
您可以使用以下正则表达式:
1 2 3 4 | new Regexp('^' + pattern).test(k) // or if the pattern isn't variable, for instance you want // to match 'X', directly use: // /^X/.test(k) |