最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Extract values from json string javascript - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Nov 22, 2013 at 10:23 user1907906 asked Nov 22, 2013 at 10:22 wesleywesley 1371 gold badge2 silver badges6 bronze badges 1
  • 1 possible duplicate of Access / process (nested) objects, arrays or JSON – Felix Kling Commented Nov 22, 2013 at 10:22
Add a comment  | 

3 Answers 3

Reset to default 9

Here's another approach

[{"id":"tag1","text":"tag1"},{"id":"tag2","text":"tag2"}].map(function(el) {
 return el.id;
});

Try this simple iteration.

var obj = [{"id":"tag1","text":"tag1"},{"id":"tag2","text":"tag2"}] ;

for (var i =0; i< obj.length ;i++) {
   console.log(obj[i].id);
}
var data = JSON.parse('[{"id":"tag1","text":"tag1"},{"id":"tag2","text":"tag2"}] ');
data[0].id
data[1].id

Try this will help you

发布评论

评论列表(0)

  1. 暂无评论