2016-05-12 64 views
1

我试图理清一个挑战,但它正在查杀。我需要将一个字符串变成一个对象。js将字符串转换为具有两个值的对象

是这样的:

{ Cape Town : 9, 
    George : 7, 
    Johannesburg : -1, 
    Port Elizabeth : 5 
    } 

,但目前它的返回我:

{ 
    Cape Town 9: undefined, 
    George 7: undefined, 
    Johannesburg -1: undefined, 
    Port Elizabeth 5: undefined 
} 

这是我当前的代码:

var str = "Look at this tomorrow it will be really cold all over the country 
in Cape Town 9, in George 7, in Port Elizabeth 5 and in, Johannesburg -1" 


var remove = str.replace("Look at this tomorrow it will be really cold all over the country:", "") 
.replace(" in", "").replace("in ", "").replace("in ", "").replace(" and in",  "") 

var properties = remove.split(', '); 
var obj = {}; 
properties.forEach(function(property) { 
    var tup = property.split(':'); 
    obj[tup[0]] = tup[1]; 
}); 

console.log(obj) 
+3

尝试调整和格式化你的问题的内容 – RomanPerekhrest

+0

''和,Johannesburg''那里有什么逗号?它不应该是“在约翰内斯堡”是一致的吗? – Cerbrus

回答

0

你有一些额外的空间之间和:最后

使其

var str = "Look at this tomorrow it will be really cold all over the country in Cape Town 9, in George 7, in Port Elizabeth 5 and in, Johannesburg -1" 
var remove = str.replace("Look at this tomorrow it will be really cold all over the country", "").replace(" in", "").replace("in ", "").replace("in ", "").replace(" and in",  "") 

另外,你需要不被结肠

var obj = {}; 
properties.forEach(function(property) { 
    var tup = property.split(/\s\d+/); 
    obj[tup[0]] = property.split(" ").pop(); 
}); 

DEMO

var str = "Look at this tomorrow it will be really cold all over the country in Cape Town 9, in George 7, in Port Elizabeth 5 and in, Johannesburg -1" 
 
remove = str.replace("Look at this tomorrow it will be really cold all over the country", "").replace(" in", "").replace("in ", "").replace("in ", "").replace(" and in",  ""); 
 
var properties = remove.split(', '); 
 
var obj = {}; 
 
properties.forEach(function(property) { 
 
    var tup = property.split(/\s\d+/); 
 
    obj[tup[0]] = property.split(" ").pop(); 
 
}); 
 
document.body.innerHTML += JSON.stringify(obj, 0, 4);

做的空间,第二裂

一个较短的版本可能是

var str = "Look at this tomorrow it will be really cold all over the country in Cape Town 9, in George 7, in Port Elizabeth 5 and in, Johannesburg -1"; 
 
var obj = {}; 
 
str.split(/\s*in\s*/).forEach(function(val, index){ 
 
    if (index == 0) return false; 
 
    val = val.replace(/\s*and\s*/, ""); //remove if there is 'and' 
 
    var tup = val.split(/\s/); 
 
    var value = tup.splice(-1); 
 
    obj[tup.join(" ")] = value.join("").replace(",", ""); 
 
}); 
 

 
document.body.innerHTML += JSON.stringify(obj, 0, 4);

+0

谢谢你们,你太棒了 –

+0

gurvinder:我建议改进'replace'链。 – Cerbrus

+0

@Cerbrus true,替换链条可以改进。我只是显示OP在他/她所做的尝试中的错误。 – gurvinder372

0

分割的空间,并弹出最后一个元素:

var str = "Look at this tomorrow it will be really cold all over the country \ 
in Cape Town 9, in George 7, in Port Elizabeth 5 and in, Johannesburg -1" 


var remove = str.replace("Look at this tomorrow it will be really cold all over the country", "") 
.replace(" in", "").replace("in ", "").replace("in ", "").replace(" and in",  "") 

var properties = remove.split(', '); 
var obj = {}; 
properties.forEach(function(property) { 
    var tup = property.split(' '); 
    var num = tup.pop(); 
    obj[tup.join(' ')] = num; 
}); 

console.log(obj); 

demo fiddle

+0

Bogdan:我会建议改进'替换'链。 – Cerbrus

1

这里有一个较短的解决方案:

var str = "Look at this tomorrow it will be really cold all over the country in Cape Town 9, in George 7, in Port Elizabeth 5 and in, Johannesburg -1", 
 
    cities = str.match(/in[\w, ]+?-?\d/g), 
 
    output = {}; 
 

 
for (var i = 0; i < cities.length; i++) { 
 
    var city = cities[i].replace(/in,? /, ''), // Remove the `in` from the current city. 
 
     split = city.split(' '),    // Split at [space] 
 
     temp = split.pop();     // Get the last item from the split string, which is the temperature. 
 
    output[split.join(' ')] = parseInt(temp); // Store the temperature for the city name. 
 
} 
 

 
console.log(output);

cities结果:
["in Cape Town 9", "in George 7", "in Port Elizabeth 5", "in, Johannesburg -1"]

然后for循环刚刚超过那些迭代并获取城市名称和温度,然后将其存储到输出。

0

使用String.replaceString.splitString.match功能的解决方案:

var str = "Look at this tomorrow it will be really cold all over the country: in Cape Town 9, in George 7, in Port Elizabeth 5 and in, Johannesburg -1", 
    items = str.replace("Look at this tomorrow it will be really cold all over the country:", "").split(','), 
    geoObj = {}; 

items.forEach(function(v) { 
    var parts = v.match(/\b([A-Z](\w|\W)+)([\d+-]+?)/g)[0].split(/\s+(?=[-+]?\d)/); 
    geoObj[parts[0]] = parseInt(parts[1]); 
}); 

console.log(geoObj); 

的输出的console.log:

{ 
    "Cape Town": 9, 
    "George": 7, 
    "Port Elizabeth": 5, 
    "Johannesburg": -1 
}