0

I am trying to create number of arrays like _temp0[],_temp1[],_temp2[] so on and I want to store values of data[] in it.

so value of data[0] goes in array_temp0[] after splitting,

data[1] goes in _temp1[] and so on

to elaborate more-

If value of data[0] is string a,b,c

then array _temp0[] should be

_temp0[0]=a 
_temp0[1]=b 
_temp0[2]=c

I wrote this function

for(var k=0;k<data.length-1;k++)
{
    window['_temp' + k] = new Array();
    alert("actual data -- >"+data[k]);
    '_temp'+k= data[k].split(',');
    alert("data after split -- >"_temp[k]);
}

but it is not working, how do I solve it?

7
  • You also need nested loops to fill up the arrays. Commented Jul 8, 2013 at 5:12
  • Your code has at least one syntax error in it. Commented Jul 8, 2013 at 5:15
  • @go-oleg i know, it's at '_temp'+k= data[k].split(','); but i don't know how to resolve it Commented Jul 8, 2013 at 5:16
  • Please explain what you are trying to do on that line. Commented Jul 8, 2013 at 5:17
  • @go-oleg trying to split value of data[0] and storing them in array _temp0 Commented Jul 8, 2013 at 5:20

2 Answers 2

1

You can do the same using javascript objects. Here is an example of how to do it.

Create an object of name '_temp':

var _temp = {};

When you iterate through 'data' variable then, you can dynamically add attributes to it,say _temp['data0'], _temp['data1'] etc, and every attribute will be an array. For that, you need to write something like:

for(var k=0;k<data.length-1;k++)
{
    _temp['data'+k] = data[k].split(',');
}

This will not create the variables identical to what you want. However, this is similar to what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

used

window['_temp'+k]= data[k].split(',');

instead of

'_temp'+k= data[k].split(','); 

and it worked, thanks to go-oleg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.