簡體   English   中英

如何將javascript字典鍵映射到新鍵

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

我有一本包含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
}

而且我有一個地圖將Windows NT 6. *映射到Windows 7或Windows 8,例如:

Microsoft Windows NT 6.1.*->windows 7
Microsoft Windows NT 6.2.*->windows 8

因此,如何將舊字典映射到新字典,其格式為:

{
  "64-bit Microsoft Windows 8": 1,
  "32-bit Microsoft Windows 8": 2,
  "64-bit Microsoft Windows 7": 7
}

謝謝

另一種選擇是使用正則表達式來匹配您的目標,如:

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

會產生:

{ '64-bit Microsoft Windows 8': 1,
  '32-bit Microsoft Windows 8': 2,
  '64-bit Microsoft Windows 7': 7 }

更新

通過將正則表達式錨定到字符串的開頭和結尾,正則表達式可能會更精確一些,以暴露可能存在的異常情況,如下所示:

`/^64-bit.+?NT\s6\.1.+$/`

可以描述為:

^            # 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

您可能還希望通過以下方式報告與目標模式匹配的目標:

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

您可以遍歷對象並根據需要更改它們。

例如:

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];
    }
}

這樣的東西應該可以工作,我還沒有測試過,但是似乎在我的腦海:)

這是一個JS小提琴,它應該可以正常使用: https : //jsfiddle.net/n3wp70me/

這是我的方法。 再次使用正則表達式值。 應該說,您要執行的操作會導致信息丟失。 Javascript對象必須具有唯一鍵,因此如果將以前兩個不同的鍵合為一個鍵,則它們的值將相互覆蓋。

例如:原始密鑰64位Microsoft Windows NT 6.1.3700和64位Microsoft Windows NT 6.1.1200都將成為64位Microsoft Windows 7

因此,您最終將丟失值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);

在鍵上使用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