我有这样的字符串
var sHTMLTable = "> 24 Hrs.,2,19,20,10,27,16,26,64,246,1|> 12 Hrs.,0,0,0,0,0,0,0,0,0,0|> 8 Hrs.,0,0,0,0,0,0,0,0,0,0|> 4 Hrs.,0,0,0,0,0,0,0,0,0,0|Total,2,19,20,10,27,16,26,64,246,1"
我想把它转换成子数组
var res = sHTMLTable.split("|").map(function (s) {
var arr = s.split(",");
return { name: arr.shift(), data: arr.map(Number) };
});
console.log(JSON.stringify(res));
哪个给我完美的输出
[{"name":"> 24 Hrs.","data":[2,19,20,10,27,16,26,64,246,1]},{"name":"> 12 Hrs.","data":[0,0,0,0,0,0,0,0,0,0]},{"name":"> 8 Hrs.","data":[0,0,0,0,0,0,0,0,0,0]},{"name":"> 4 Hrs.","data":[0,0,0,0,0,0,0,0,0,0]},{"name":"Total","data":[2,19,20,10,27,16,26,64,246,1]}]
但由于我使用了 .map
函数,它会在 IE 中抛出错误。 我怎样才能在 IE 中运行并获得相同的结果?
最佳答案
在 IE 中查看解决所有数组函数的精彩答案 here . 在你的代码上面写
if (!('map' in Array.prototype)) {
Array.prototype.map= function(mapper, that /*opt*/) {
var other= new Array(this.length);
for (var i= 0, n= this.length; i<n; i++)
if (i in this)
other[i]= mapper.call(that, this[i], i, this);
return other;
};
}
或者像这样简单地使用jQuery.map
jQuery.map(a, function( ) { //what ever you want todo .. }
关于javascript - 在javascript IE中从字符串创建子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18331878/