Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"....]
share|improve this question

4 Answers 4

up vote 8 down vote accepted

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;
});
share|improve this answer
    
Just curious (I don't use jQuery) - is that $.map function asynchronous? –  JKing Apr 11 '12 at 10:24
    
@JKing, nope, it isn't. It builds and returns the array synchronously. The function it takes as an argument is not a callback, it's used to project the items of the new array. –  Frédéric Hamidi Apr 11 '12 at 10:27
    
ah. I've been spending too much time in node.js... every anonymous function passed as an arg to another function makes me think "Weeee! Asynchronous event driven programming!" Thanks for nothing, jQuery! –  JKing Apr 11 '12 at 10:41
    
I love this solution. I learned something. –  Dutchie432 Apr 11 '12 at 13:01
var t_array = [];

for (var i=0; i< json_array.length; i++)
    t_array.push(json_array[i].text);
share|improve this answer

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

share|improve this answer

something like:

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.