user1509674
user1509674

Reputation: 133

how to push a list of number into an array using javascript

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

Upvotes: 0

Views: 4134

Answers (4)

Sunil Kumar
Sunil Kumar

Reputation: 11

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;

Upvotes: 0

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

Upvotes: 1

Aameer
Aameer

Reputation: 1376

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.

Upvotes: 1

Midhun Murali
Midhun Murali

Reputation: 2151

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

Upvotes: 0

Related Questions