我有一个包含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
}
由于
答案 0 :(得分:1)
另一种选择是使用正则表达式来匹配您的目标,如:
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)
}
}
答案 1 :(得分:0)
您可以遍历对象并根据需要进行更改。
例如:
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/
答案 2 :(得分:0)
这是我的方法。再次使用正则表达式值。 应该说,你要求做的事情会导致信息丢失。 Javascript对象必须具有唯一键,因此如果将以前是2个不同键的键折叠成一个,则它们的值将相互覆盖。
例如: 原始钥匙 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);
答案 3 :(得分:0)
在键上使用for..in
和String.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
} */