1

I have string like

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"

I want it to convert to Sub Array

var res = sHTMLTable.split("|").map(function (s) {
    var arr = s.split(",");
    return { name: arr.shift(), data: arr.map(Number) };
});
console.log(JSON.stringify(res));

Which give me perfect output

[{"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]}] 

But it throws error in IE because of .map function i used . how can i make it work in IE by achieving same result ?

Shaggy
  • 5,422
  • 28
  • 98
  • 163

5 Answers5

2

Use $.map() since you are using jQuery

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 = $.map(sHTMLTable.split("|"), function (s) {
    var arr = s.split(",");
    return { name: arr.shift(), data: $.map(arr, Number) };
});
console.log(JSON.stringify(res));
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

Look a wonderful answer of solution all arrays function in IE here. In above of your code write

 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;
};
}

Or simply use jQuery.map like this

 jQuery.map(a, function( ) { //what ever you want todo .. }
Community
  • 1
  • 1
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
1

Just replace Array.prototype.map with $.map on the both places:

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 = $.map(sHTMLTable.split("|"), function (s) {
    var arr = s.split(",");
    return { name: arr.shift(), data: $.map(arr, (Number)) };
});
console.log(JSON.stringify(res));
Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
1

IE doesn't support map(). Check MDN (Mozilla Developer Network):

map - MDN

Looks like IE added support for map() in version 9.

Amith
  • 1,424
  • 1
  • 10
  • 22
1

Here is my version

 sHTMLTable = sHTMLTable.split('|');
 for(var i = 0, l = sHTMLTable.length; i < l ; i++){
    temp = sHTMLTable[i].replace(',','_').split('_');
    sHTMLTable[i] = {
       name : temp[0],
       data : temp[1]
    }
 }

Check it here in http://jsfiddle.net/GvyuV/

Exception
  • 8,111
  • 22
  • 85
  • 136