Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract values from json string javascript

I am using a multiselect element from select2 to enter multiple "tags". When I want to get the value from the element I get something like this (for tag1 en tag2 which I entered in the box):

[{"id":"tag1","text":"tag1"},{"id":"tag2","text":"tag2"}] 

How do I get the result from text in an array something like this:

[0] = "tag1"
[1] = "tag2"

And how do I reverse this process?

like image 739
wesley Avatar asked Dec 06 '22 04:12

wesley


1 Answers

Here's another approach

[{"id":"tag1","text":"tag1"},{"id":"tag2","text":"tag2"}].map(function(el) {
 return el.id;
});
like image 98
nullpotent Avatar answered Dec 14 '22 12:12

nullpotent