I have a dictionary contains windows os versions,like:
{
"64-bit Microsoft Windows NT 6.2.9200": 1,
"32-bit Microsoft Windows NT 6.2.9137": 2,
"64-bit Microsoft Windows NT 6.1.3700": 3,
"64-bit Microsoft Windows NT 6.1.1200": 4
}
and I have a map to map windows NT 6.* to windows 7 or windows 8, like:
Microsoft Windows NT 6.1.*->windows 7
Microsoft Windows NT 6.2.*->windows 8
So how can I map the old dictionary to the new one with the format:
{
"64-bit Microsoft Windows 8": 1,
"32-bit Microsoft Windows 8": 2,
"64-bit Microsoft Windows 7": 7
}
Thanks
Another option would be to use Regular Expressions to match your targets, as in:
var maps = {
// result => RegExp
'64-bit Microsoft Windows 7': /64-bit.+?NT\s6\.1/,
'32-bit Microsoft Windows 8': /32-bit.+?NT\s6\.2/,
'64-bit Microsoft Windows 8': /64-bit.+?NT\s6\.2/
};
var test_data={
"64-bit Microsoft Windows NT 6.2.9200": 1,
"32-bit Microsoft Windows NT 6.2.9137": 2,
"64-bit Microsoft Windows NT 6.1.3700": 3,
"64-bit Microsoft Windows NT 6.1.1200": 4
};
var result={};
for(key in test_data){
for(target in maps){
if(maps[target].test(key)){
if(!result[target]){
result[target]=0;
}
result[target]+=test_data[key];
break;
}
}
}
console.dir(result);
which will produce:
{ '64-bit Microsoft Windows 8': 1,
'32-bit Microsoft Windows 8': 2,
'64-bit Microsoft Windows 7': 7 }
The regexes could be a little more precise, to expose possible outlying cases, by anchoring them to the start and end of the string, as in:
`/^64-bit.+?NT\s6\.1.+$/`
which can be described as:
^ # beginning of target string
64-bit # literal '64-bit'
.+? # one or more chars, non-greedy
NT # literal 'NT'
\s # literal space
6\.1 # literal '6.1'
.+ # one or more chars, greedy
$ # end of target string
You may also wish to report targets that do not match your target patterns by refactoring to:
for(key in test_data){
var found=false;
for(target in maps){
if(maps[target].test(key)){
if(!result[target]){
result[target]=0;
}
result[target]+=test_data[key];
found=true;
break;
}
}
if(!found){
console.warn('encountered unknown record at key "%s"',key)
}
}
You can iterate through the object and change them as necessary.
For example:
var myObj = {
"64-bit Microsoft Windows NT 6.2.9200": 1,
"32-bit Microsoft Windows NT 6.2.9137": 2,
"64-bit Microsoft Windows NT 6.1.3700": 3,
"64-bit Microsoft Windows NT 6.1.1200": 4
};
var myNewObj = {
"64-bit Microsoft Windows 7" : 0,
"64-bit Microsoft Windows 8" : 0
};
for (var key in myObj){
if (key.indexOf("Microsoft Windows NT 6.1") > -1){
myNewObj["64-bit Microsoft Windows 7"] += myObj[key];
} else if (key.indexOf("Microsoft Windows NT 6.2") > -1){
myNewObj["64-bit Microsoft Windows 8"] += myObj[key];
} else {
myNewObj[key] = myObj[key];
}
}
Something like that should work, I haven't tested it but it seems right in my head :)
Here's a JS Fiddle, it should work right: https://jsfiddle.net/n3wp70me/
Here is my approach. Once again using regex values. It should be said though that what you're asking to do will result in a loss of information. Javascript objects must have unique keys, so if you collapse what used to be 2 distinct keys into one, then their values will overwrite each other.
for example: original keys 64-bit Microsoft Windows NT 6.1.3700 and 64-bit Microsoft Windows NT 6.1.1200 Will both become 64-bit Microsoft Windows 7
So you'll end up losing the value 3, it will be overwritten to the value 4.
var dict = {"64-bit Microsoft Windows NT 6.2.9200":1,"32-bit Microsoft Windows NT 6.2.9137":2,"64-bit Microsoft Windows NT 6.1.3700":3,"64-bit Microsoft Windows NT 6.1.1200":4};
var w7 = /Microsoft Windows NT 6.1.*/;
var w8 = /Microsoft Windows NT 6.2.*/;
var i, len, outKey, key, keys = Object.keys(dict), out = {};
for (i = 0, len = keys.length; i < len; i++) {
key = keys[i];
outKey = key.replace(w7, 'Microsoft Windows 7');
outKey = outKey.replace(w8, 'Microsoft Windows 8');
out[outKey] = dict[key];
}
console.log(out);
Using for..in
and String.prototype.replace
on keys you could put together something like this
var old_dict = {
"64-bit Microsoft Windows NT 6.2.9200": 1,
"32-bit Microsoft Windows NT 6.2.9137": 2,
"64-bit Microsoft Windows NT 6.1.3700": 3,
"64-bit Microsoft Windows NT 6.1.1200": 4
};
var new_dict = (function (d1) {
var k1, k2, d2 = {},
re = /NT (\d+\.\d+)\.\d+/,
version_map = { // major.minor version number mappings
'6.1': '7',
'6.2': '8'
};
function replacer($0, $1) { // if we don't have a map, default to what was put in
return version_map[$1] || $0;
}
for (k1 in d1) {
k2 = k1.replace(re, replacer);
d2[k2] = (d2[k2] || 0) + d1[k1]; // add together if already exists
}
return d2;
}(old_dict));
/* new_dict looks like {
"64-bit Microsoft Windows 8": 1,
"32-bit Microsoft Windows 8": 2,
"64-bit Microsoft Windows 7": 7
} */
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.