Prasanna
Prasanna

Reputation: 11544

Extracting data from JsonArray using JS/jQuery

I have a JsonArray something like this.

var json_array = [{ "text": "id", "size": 4}, { "text": "manifesto", "size": 4}, { "text": "also", "size": 4}, { "text": "leasing", "size": 4}, { "text": "23", "size": 4}];

Is there anyway to get me all the "text" property of this json_array in another array using Javascript/jQuery?

like:

var t_array = ["id","manifesto","also"....]

Upvotes: 3

Views: 1625

Answers (4)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263047

You can use $.map() to project the relevant information from your existing array into a new one:

var t_array = $.map(json_array, function(item) {
    return item.text;
});

Upvotes: 9

Benoît
Benoît

Reputation: 7427

something like:

var t_array = [];
$.each(json_array,function(i,o) {
  t_array.push(o.text);
})

http://jsfiddle.net/bouillard/2c66t/

Upvotes: 1

Dutchie432
Dutchie432

Reputation: 29160

I think you'll need to build t_array by looping through json_array

Upvotes: 1

JKing
JKing

Reputation: 847

var t_array = [];

for (var i=0; i< json_array.length; i++)
    t_array.push(json_array[i].text);

Upvotes: 2

Related Questions