Remove some elements from a map in Javascript

how can I remove all key / value pairs from the next card, where the key starts with X.

var map = new Object(); map[XKey1] = "Value1"; map[XKey2] = "Value2"; map[YKey3] = "Value3"; map[YKey4] = "Value4"; 

EDIT

Is there any way through regex, possibly using ^. Something like map [^ XKe], where the key starts with "Xke" instead of "X"

+4
source share
4 answers

You can Object.key over map keys using Object.key .

The simplest solution:

DEMO HERE

 Object.keys(map).forEach(function (key) { if(key.match('^'+letter)) delete obj[key]; }); 

So here is another version of removeKeyStartsWith with regex, as you 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); 

A solution with Regex will cover your needs, even if you use the letter = Xke, as you said, but for another solution without Regex you will need to replace:

Key[0]==letter with key.substr(0,3)==letter

+2
source

I would suggest:

 function removeKeyStartsWith(obj, letter) { for (var prop in obj) { if (obj.hasOwnProperty(prop) && prop[0] == letter){ delete obj[prop]; } } } 

JS Fiddle demo .

By the way, it is usually simpler (and, apparently, considered "best practice") to use a literal object rather than a constructor, so you should show the following (even if for some reason you prefer new Object() :

 var map = { 'XKey1' : "Value1", 'XKey2' : "Value2", 'YKey3' : "Value3", 'YKey4' : "Value4", }; 

JS Fiddle demo .

If you really want to use regular expressions (but why?), Then the following works:

 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 Fiddle demo .

Finally (at least for now) an approach that extends the Object prototype, allowing the user to search for a property that begins with a given string, ends with a given string or (using both startsWith and endsWith ) - a given string (with or without case sensitivity:

 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 Fiddle demo .

Literature:

+10
source

Background

Assuming your original input:

 var map = new Object(); map[XKey1] = "Value1"; map[XKey2] = "Value2"; map[YKey3] = "Value3"; map[YKey4] = "Value4"; 

And suppose the pattern variable will contain what you want to filter the keys (for example, "X" , "Y" , "prefixSomething" , ...).

Solution 1 - Using jQuery to filter and create a new object

 var clone = {}; $.each(map, function (k, v) { if (k.indexOf(pattern) == 0) { // k not starting with pattern clone[k] = v; } }); 

Solution 2 - Using pure ECMAScript and creating a new object

 var clone = {}; for (var k in map) { if (map.hasOwnProperty(k) && (k.indexOf(pattern) == 0)) { clone[k] = map[k]; } } 

Solution 3 - Using pure ECMAScript and using the source object

 for (var k in map) { if (map.hasOwnProperty(k) && (k.indexOf(pattern) == 0)) { delete map[k]; } } 

Or, in modern browsers:

 Object.keys(map).forEach(function (k) { if (k.indexOf(pattern) == 0) { delete map[k]; } }); 

Update - using regular expressions to match pattern

Instead of using the following for matching, if the key k begins with a letter with:

 k[0] == letter // to match or letter 

or to match, if the k key starts with the line:

 k.indexOf(pattern) // to match a string 

you can use regex instead:

 new Regexp('^' + pattern).test(k) // or if the pattern isn't variable, for instance you want // to match 'X', directly use: // /^X/.test(k) 
+1
source

You can easily get it that way.

 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); 

This is an easy way to remove it.

+1
source

Source: https://habr.com/ru/post/1500363/


All Articles