It's actually very straight forward with jQuery's $.map
(它实际上非常直接使用jQuery的$.map
)
var arr = $.map(obj, function(el) { return el });
FIDDLE(小提琴)
and almost as easy without jQuery as well, converting the keys to an array and then mapping back the values with Array.map
(并且在没有jQuery的情况下几乎同样容易,将键转换为数组,然后使用Array.map
将值映射回来)
var arr = Object.keys(obj).map(function(k) { return obj[k] });
FIDDLE(小提琴)
That's assuming it's already parsed as a javascript object, and isn't actually JSON, which is a string format, in that case a run through JSON.parse
would be necessary as well.(这假设它已经被解析为javascript对象,并且实际上不是JSON,这是一种字符串格式,在这种情况下,也需要运行JSON.parse
。)
In ES2015 there's Object.values
to the rescue, which makes this a breeze(在ES2015中有拯救的Object.values
,这使得这一切变得轻而易举)
var arr = Object.values(obj);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…