Steven De Groote Steven De Groote - 1 year ago 75
Javascript Question

join array of objects by using one property

I've got an array of objects with a few properties, such as 'value' and 'label'.

array.join(' ')
of course gives me "[object] [object]", but instead I need to get a string of the 'value' properties of all objects, separated by a space.

What is the shortest way to do this, and is this possible without writing a for loop?

Answer Source

Try using - not used because of IE < 9 support

For JSON.stringify() - use json2 for old browser support

$.map(array, function(obj){return JSON.stringify(obj)}).join(' ')

Update: To get the value properties

var string = $.map(array, function(obj){
    return obj.value
}).join(' ');

Demo: Fiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download