Page 1 of 2

String Parsing

Posted: Mon Mar 09, 2015 5:51 pm
by Abhijeet Singh

How can I convert this

[{"party":"Abc"},{"party":"Pqr"},{"party":"jky"}]

to this

["Abc","Pqr","jky"]

via javascript


String Parsing

Posted: Mon Mar 09, 2015 6:28 pm
by sebastian

I think you'd do something like.

var arr = [{"party":"Abc"},{"party":"Pqr"},{"party":"jky"}]
var arrString = arr.toString();

If it doesn't work try this. Let me know if either one worked for you.

var arrString = JSON.stringify(arr);


String Parsing

Posted: Mon Mar 09, 2015 6:48 pm
by sebastian

Are you trying to turn it into a string or an object array?


String Parsing

Posted: Mon Mar 09, 2015 6:52 pm
by Abhijeet Singh

"party" is the object here

all i want is to get an array of the values

["Abc","Pqr","jky"]


String Parsing

Posted: Mon Mar 09, 2015 6:55 pm
by sebastian

try this then

var arrString = JSON.parse(arr);

instead of stringifying it


String Parsing

Posted: Mon Mar 09, 2015 6:57 pm
by Abhijeet Singh

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]


String Parsing

Posted: Mon Mar 09, 2015 7:01 pm
by sebastian

Hmm. We'll I'm working on storing objects arrays in local storage myself and trying to modify them as well when they are stored. I've been having some issues myself. When you have it as a string what does the console tell you?


String Parsing

Posted: Mon Mar 09, 2015 7:20 pm
by sebastian

Here is an array I created and stored it to a local storage variable called test.

function Person (name, age) {
this.name = name;
this.age = age;
}

var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);

var fx = JSON.stringify(family);

localStorage.setItem("test", fx);

var x = JSON.parse(localStorage.getItem("test"));
for(i = 0; i < x.length; i++){
console.log(x.name);
}

the console reads out
alice
bob
michelle
timmy

I hope this helps


String Parsing

Posted: Mon Mar 09, 2015 7:21 pm
by Egor Kotov6832188

Hello Abhijeet.
We have provided answer via e-mail


String Parsing

Posted: Mon Mar 09, 2015 7:21 pm
by sebastian

Here is an array I created and stored it to a local storage variable called test.

function Person (name, age) {
this.name = name;
this.age = age;
}

var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);

var fx = JSON.stringify(family);

localStorage.setItem("test", fx);

var x = JSON.parse(localStorage.getItem("test"));
for(i = 0; i < x.length; i++){
console.log(x.name);
}

the console reads out
alice
bob
michelle
timmy

I hope this helps