使用一个属性连接对象数组 [英] join array of objects by using one property
本文介绍了使用一个属性连接对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有一些属性的对象数组,例如'value'和'label'.
I've got an array of objects with a few properties, such as 'value' and 'label'.
array.join(' ')
当然会给我"[object] [object]",但是我需要获取所有对象的'value'属性的字符串,并用空格分隔.
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.
执行此操作的最短方法是什么,并且无需编写for循环就可以实现吗?
What is the shortest way to do this, and is this possible without writing a for loop?
推荐答案
尝试使用 jQuery.map() - Array.map()由于IE<而未使用9支持
Try using jQuery.map() - Array.map() not used because of IE < 9 support
对于 JSON.stringify()-使用 json2 获得旧版浏览器支持
For JSON.stringify() - use json2 for old browser support
$.map(array, function(obj){return JSON.stringify(obj)}).join(' ')
更新:获取值属性
var string = $.map(array, function(obj){
return obj.value
}).join(' ');
演示:小提琴
这篇关于使用一个属性连接对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文