我试图理清一个挑战,但它正在查杀。我需要将一个字符串变成一个对象。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)
尝试调整和格式化你的问题的内容 – RomanPerekhrest
''和,Johannesburg''那里有什么逗号?它不应该是“在约翰内斯堡”是一致的吗? – Cerbrus