0

Hello,

I have the following string

    [null,null,null],["Paul","2293",1],["eric","2099",0]

which I will define as:

    var dataparsed ='[null,null,null],["Paul","2293",1],["eric","2099",0]';

and would like to construct three different arrays in the following format:

var opponentName = new Array(null, "Paul","eric");

var numberVal =new Array(null,  2293,2099);

var outComeVal = new Array(null,  1,0);

Any ideas on how this can be done in Javascript?

I appreciate any thoughts on this.

Mossa--

Edited by mbarandao: revised

4
Contributors
7
Replies
44
Views
2 Years
Discussion Span
Last Post by mbarandao
0

Hi

There is probably a more elegant solution (I'm no JS expert), but the following provides the output that you mention:

            var myString = '[null,null,null],["Paul","2293",1],["eric","2099",0]';

            //Get first set of data
            var set1 = myString.substring(myString.indexOf("[") + 1, myString.indexOf("]")).split(",");

            //Remove set1 from myString
            myString = myString.replace('[' + set1 +'],', '');

            //Get second set of data
            var set2 = myString.substring(myString.indexOf("[") + 1, myString.indexOf("]")).split(",");

            //Remove set2 from myString
            myString = myString.replace('[' + set2 + '],', '');

            //Get third set of data
            var set3 = myString.substring(myString.indexOf("[") + 1, myString.indexOf("]")).split(",");

            //Put your data together
            var opponentName = new Array(set1[0], set2[0], set3[0]);
            var numberVal =  new Array(set1[1],  set2[1], set3[1]);
            var outComeVal = new Array(set1[2], set2[2], set3[2]);

            alert(opponentName);
            alert(numberVal);
            alert(outComeVal);

HTH

Edited by djjeavons

0

djjeovons,

I appreciate your thoughts on this. It appears to be exactly what I am trying to achieve. I will post back the outcome after testing.

Thanks again!
Mossa

0

I am trying to remove the double quotation from all numeric values in the array.

var numberVal =new Array(null,  "2293","2099");

I have tried: (with no success)

var numberVal_Raw=numberVal.replace(/[\"\[\]]/g,"");

I need it to be:

var numberVal =new Array(null,  2293,2099);

Edited by mbarandao

0

numberVal.replace(/"/g, '');

ignore this. This is not working on array

Edited by lps: ignore this. This is not working on array

0

You have a string in dataparsed - why not make it an array (or an object)?

function toInt(str){ return (str !== null) ? parseInt(str) : null; }

//Change string to this
var dataparsed = [[null,null,null],["Paul","2293",1],["eric","2099",0]];

var opponentName = dataparsed.map(function(v,i) { return v[0]; });
var numberText = dataparsed.map(function(v,i) { return v[1]; });
var numberVal = numberText.map(toInt);
var outComeVal = dataparsed.map(function(v,i) { return v[2]; });

Test with the following

console.log(opponentName);
console.log(numberText);
console.log(numberVal);
console.log(outComeVal);

Note that .map doesn't work on IE9 and earlier. I believe there are workarounds. Don't you just love IE?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.