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.

Possible Duplicate:
Accessing properties of an array of objects

[{
    'id':1,
    'name':'john'
},{
    'id':2,
    'name':'jane'
}........,{
    'id':2000,
    'name':'zack'
}]

what's the best way to get:

['john', 'jane', ...... 'zack']

must i loop through and push item.name to another array? is there a simple function to do it?

share|improve this question

marked as duplicate by Felix Kling, Rory McCrossan, Mario, BeRecursive, Eric J. Dec 20 '12 at 16:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Are you sure you need to convert it to an array like that? Oftentimes, doing a conversion like this is an indication that you may be going about your underlying problem the wrong way. It might be useful to take a step back and make sure that's not the case here. –  Shauna Dec 20 '12 at 13:25
    
@shauna my problem is this. im using bootstrap's typeahead it only access the second format: which is a array of string. my ajax calls will return the first format cuz i need the ID. i can of cuz go and extend boostrap's typeahead but rather do this: send the 2nd format to bootstrap. just keep the first format around and when user selects a choice check this first object and use the id as needed. –  PK. Dec 20 '12 at 13:36

4 Answers 4

up vote 9 down vote accepted

If your array of objects is items, you can do:

var names = items.map(function(item) {
    return item['name'];
});

Documentation: map()

share|improve this answer
    
the question is not tagged with jQuery –  Bruno Dec 20 '12 at 13:26
    
@bru.scopelliti map() is native JavaScript and not just jQuery. –  Sirko Dec 20 '12 at 13:27
    
@bru.scopelliti - my answer has nothing to do with jQuery.. –  techfoobar Dec 20 '12 at 13:27
    
that's not jQuery. That's just modern javascript. Cool, huh? –  Mark Hubbart Dec 20 '12 at 13:28
    
@bru.scopelliti: I don't see any jQuery there. –  Cerbrus Dec 20 '12 at 13:28

Use the map() function native on JavaScript arrays:

var yourArray = [ {
    'id':1,
    'name':'john'
},{
    'id':2,
    'name':'jane'
}........,{
    'id':2000,
    'name':'zack'
}];

var newArray = yourArray.map( function( el ){ 
                                return el.name; 
                               });
share|improve this answer

You can use this function:

function createStringArray(arr, prop) {
   var result = [];
   for (var i = 0; i < arr.length; i += 1) {
      result.push(arr[i][prop]);
   }
   return result;
}

Just pass the array of objects and the property you need. The script above will work even in old EcmaScript implementations.

share|improve this answer

You can do this to only monitor own properties of the object:

var arr = [];

for (var key in p) {
    if (p.hasOwnProperty(key)) {
        arr.push(p[key]);
    }
}
share|improve this answer

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