简体   繁体   English

如何将javascript字典键映射到新键

[英]how to map javascript dictionary keys to new keys

I have a dictionary contains windows os versions,like: 我有一本包含Windows OS版本的字典,例如:

{
  "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: 而且我有一个地图将Windows NT 6. *映射到Windows 7或Windows 8,例如:

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 }

UPDATED 更新

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/ 这是一个JS小提琴,它应该可以正常使用: 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. Javascript对象必须具有唯一键,因此如果将以前两个不同的键合为一个键,则它们的值将相互覆盖。

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 例如:原始密钥64位Microsoft Windows NT 6.1.3700和64位Microsoft Windows NT 6.1.1200都将成为64位Microsoft Windows 7

So you'll end up losing the value 3, it will be overwritten to the value 4. 因此,您最终将丢失值3,它将被覆盖为值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 在键上使用for..inString.prototype.replace可以将类似的内容放在一起

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
} */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM