0

suppose I have printed a list of values and stored in a variable.The variable is of string type.Now I need to push those elements stored in a variable into an array.How can do this using javascript?

private var data : String;
         private var dataarray:Array; 
     for(var k : int = 0; k < StockItemsProperties_list.Count; k++)  
            {
   data=StockItemsProperties_list[k].InnerText+"\t";
      dataarray=new Array();
            dataarray.Push(data);
            Debug.Log("Elements in the array"+dataarray);
       }
3
  • How the value is stored in the varaible ? Commented Oct 20, 2014 at 6:26
  • @Midhun : the values stored in the variable are numbers Commented Oct 20, 2014 at 7:17
  • in the variable dataarray the number in the "data" is getting displayed.Now Iam getting the output as 1 a 2 c But i need the output as 1 a Commented Oct 20, 2014 at 7:21

4 Answers 4

1

String.prototype.split() && Array.prototype.concat()

var arr = ["xyz"];
var str1 = "foo,bar,baz";
var str2 = "qwer";

with non-empty string separator:

var str1_separated = str1.split(",");
// str1_separated == ["foo","bar","baz"]

with empty string separator:

var str2_separated = str2.split("");
// str2_separated == ["q","w","e","r"]

pushing new values to array:

arr.concat(str1_separated, str2_separated);
// arr == ["xyz","foo","bar","baz","q","w","e","r"]
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand you correctly then you are looking for something like this.

var x = "abc"
var y = "lmn"
var array =[]
array[0]= x;
array[1]= y;

the array will contain ["abc", "lmn"].

If your question is that you have saved all the values in the same var then as @Midhun said you need a delimiter example

var x = "abc,lmn"
array=x.split(',')

here I am using , as a delimiter Hope that helps.

Comments

0

Hope you have stored the values in a variable using some symbol to sepreate the items . If so you can use the javascript split() to convert string to list of items. The Split function will return you an array with the list of items

Please have look at the below url http://www.w3schools.com/jsref/jsref_split.asp

1 Comment

dataarray=new Array(); dataarray.Push(data); datalist=dataarray.split(); When I coded the its giving an error msg saying split() doesnt exist in Array()
0

Declare the Variable which you want to push int in array.

var array= []; var a = +1; var b = +2; var c = +3; array[0]=a; array[1]=b; array[2]=c;

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.