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

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

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

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

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

ignore this. This is not working on array

var numberVal = new Array(set1[1], set2[1].replace(/[\"[]]/g,""), set3[1].replace(/[\"[]]/g,""));

Member Avatar for diafol

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?

Diafol,

It is good to have you chime-in on this. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.