js字符串转换为具有两个值的对象
js string into an object with two values
我正试图解决一个挑战,但它很致命。我需要把一个字符串变成一个对象。
像这样:
{ 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)
这里有一个较短的解决方案:
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
循环只对这些进行迭代,并从中获取城市名称和温度,然后将其存储到输出中。
在末尾的:
之间有一些额外的空间
使其成为
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();
});
演示
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 );
按空格拆分并弹出最后一个元素:
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);
演示小提琴
使用String.replace
、String.split
和String.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
}
相关文章:
- 正在更新mongod中两个对象内部的数组
- 添加两个对象以获取值的总和
- 如何使用javascript合并两个对象数组
- 在JavaScript中相等两个对象.在更改一时,秒会自动更改
- 如何使用AngularJs比较两个对象的JSON
- 如何在javascript中合并两个对象数组
- JavaScript - 合并两个对象数组并根据属性值删除重复数据
- Javascript如何确定两个对象是否相同
- 比较两个对象数组
- Javascript tests - 如何比较 Jasmine 中的两个对象
- JavaScript:为什么 array.push() 附加两个对象而不是一个对象
- 两个对象相等,但 js 确实说假
- 两个对象之间的Javascript原型
- 使用angularjs或javascript的两个对象数组的交集
- 合并两个对象而不覆盖
- 如何获取具有相同值的两个对象,并将其视为一个对象
- 比较两个对象数组中的重复对象,并在其'这在JavaScript中不是重复的
- 比较underscorejs中的两个对象数组
- 查找/合并具有共同属性的两个对象
- 有没有一个lodash函数可以合并两个对象并删除其中一个对象的属性(如果它们没有)'不存在于另一个中