Converting Array to string

rito1
rito1 used Ask the Experts™
on
Hi All,

I have the following code:

var firstName = "Rit";
var surname = "Smith";
var email = "my@email.com";
var UserID = 1;

strUserData = "[{N:\"Aarav Adalik\",E:\"Aarav.Adalik@test.com\",I:107},{N:\"Aardra Adamecki\",E:\"Aardra.Adamecki@test.com\",I:108},{N:\"Aariz Albin\",E:\"Aariz.Albin@test.com\",I:109},{N:\"Aarti Aleksandra\",E:\"Aarti.Aleksandra@test.com\",I:110}]";

var newUserStoreObj = {
                N: firstName + " " + surname,
                E: firstName + " " + surname + " (" + email + ")",
                I: UserID};

var LocalUserData = eval(strLocalUserData);
LocalUserData.push(newUserStoreObj);

alert(JSON.Stringify(LocalUserData))

Open in new window



The above code evaluates the string strUserData as the array LocalUserData, appends the newUserStoreObj to the array LocalUserData. Then I want to convert it back to a string so that I can save it into LocaStorage.

I get an error on JSON.Stringify(LocalUserData).

Can anyone advise what I am doing wrong?

Thanks,

Rit
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Change: JSON.Stringify to JSON.stringify
Change: eval(strLocalUserData) to eval(strUserData)

var firstName = "Rit";
var surname = "Smith";
var email = "my@email.com";
var UserID = 1;

strUserData = "[{N:\"Aarav Adalik\",E:\"Aarav.Adalik@test.com\",I:107},{N:\"Aardra Adamecki\",E:\"Aardra.Adamecki@test.com\",I:108},{N:\"Aariz Albin\",E:\"Aariz.Albin@test.com\",I:109},{N:\"Aarti Aleksandra\",E:\"Aarti.Aleksandra@test.com\",I:110}]";

var newUserStoreObj = {
                N: firstName + " " + surname,
                E: firstName + " " + surname + " (" + email + ")",
                I: UserID};

var LocalUserData = eval(strUserData);
LocalUserData.push(newUserStoreObj);

alert(JSON.stringify(LocalUserData))¿

Open in new window

Author

Commented:
It was the CAP S that was killing it!.. thanks :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial