3

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"....]

4 Answers 4

9

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;
});
Sign up to request clarification or add additional context in comments.

3 Comments

Just curious (I don't use jQuery) - is that $.map function asynchronous?
@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.
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!
2
var t_array = [];

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

Comments

1

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

Comments

1

something like:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.