1

How to get separate values of array in javascript?

in one page:

var c=new Array(a); (eg: a={"1","2"}) window.location="my_details.html?"+ c + "_"; and in my_details.html :

my_details.htm:

var q=window.location.search;     
alert("qqqqqqqqqqqqq " + q);    
var arrayList = (q)? q.substring(1).split("_"):[];       
var list=new Array(arrayList);     
alert("dataaaaaaaaaaaa " +  decodeURIComponent(list)   + "llll " );  

But i am not able to get individual array value like list[0] etc
How to get it?

thanks
Sneha

1 Answer 1

0

decodeURIComponent() will return you a String; you need to do something like:

var delim = ",",
    c = ["1", "2"];

window.location = "my_details.html?" + c.join(delim);

And then get it back out again:

var q = window.location.search,
    arrayList = (q)? q.substring(1).split("_"):[],       
    list = [arrayList];     
    arr = decodeURIComponent(list).split(delim);

This will use the value of delim as the delimiter to make the Array a String. We can then use the same delimiter to split the String back into an Array. You just need to make sure delim is available in the scope of the second piece of code.

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

5 Comments

If you do console.log(decodeURIComponent(list)) what do you see? Are the data items delimited by spaces?
But Now I am facing the problem with [1, value list[0] value. how to resolve that?
Do you mean that the Array you get out contains a nested Array?
nope, the value of arr[0] is "[1" instead of just "1". The value "[" gets added to arr[0].!!
Sorry, my mistake. Try removing list = [arrayList];. And using arr = decodeURIComponent(arrayList).split(delim);.

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.