Javascript 转换地图<;字符串,字符串>;在jquery中插入数组
我在java中有如下映射:Javascript 转换地图<;字符串,字符串>;在jquery中插入数组,javascript,jquery,json,dictionary,Javascript,Jquery,Json,Dictionary,我在java中有如下映射: "{one=Print, two=Email, three=Download, four=Send to Cloud}"; 我需要在jquery中将上面的字符串转换为数组,循环数组并获取相应的键和值,使用、和方法 var str=“{one=Print,two=Email,three=Download,three=Send to Cloud}”; str //删除起始和结束处的空间 .trim() //获取不带“{”和“}”的字符串` .切片(1,-1) //除以
"{one=Print, two=Email, three=Download, four=Send to Cloud}";
我需要在jquery中将上面的字符串转换为数组,循环数组并获取相应的键和值,使用、和方法
var str=“{one=Print,two=Email,three=Download,three=Send to Cloud}”;
str
//删除起始和结束处的空间
.trim()
//获取不带“{”和“}”的字符串`
.切片(1,-1)
//除以``
.split(“,”)
//迭代数组
.forEach(功能(v){
//分开`=`
var val=v.trim().split('=');
log('键:'+val[0]+”,值:“+val[1]”)
})
下面是一个简单的破解:
let arr = jsons.replace('{','').replace('}','').split(',')
arr.map((each)=>{let newVal = each.split('='); return {key: newVal[0], value: newVal[1]}})
试试这个:
function convertString(string) {
return string.split(', ').map(function(a) {
var kvArr = a.split('=');
return {key: kvArr[0], value: kvArr[1]};
};
}
函数转换字符串(字符串){
string=string.slice(1,string.length-1);
返回string.split(',').map(函数(a){
var kvArr=a.split('=');
返回{key:kvArr[0],value:kvArr[1]};
});
}
警报(JSON.stringify(convertString({one=Print,two=Email,three=Download,three=Send to Cloud}))代码>问题出在哪里?显示到目前为止您已经尝试了什么?ie8中不支持.reduce()的可能重复项。是否有其他替代方案this@user3044552:检查polyfill: