如何在对象数组上使用jQuery.map()来返回数组数组 [英] How to use jQuery.map() on array of objects to return array of arrays
本文介绍了如何在对象数组上使用jQuery.map()来返回数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用jQuery使用map将对象数组转换为数组数组。
I would like to use jQuery to convert an array of objects to array of arrays using map.
例如,如果我有这个:
var ObjArr = [{ a:1,b:2 },{ a:2,b:3 },{ a:3,b:4 }];
var ArrArr = $.map(ObjArr, function(n,i){
return [ n.a, n.b ];
});
结果如下:
ArrArr = [[1,2],[2,3],[3,4]]
推荐答案
使用 jQuery.map()
(docs) 和 map()
(docs) 方法你需要重复包装返回值:
With the jQuery.map()
(docs) and map()
(docs) methods you need to double wrap the return value:
var ArrArr = $.map(ObjArr, function(n,i){
return [[ n.a, n.b ]];
});
...否则由于某种原因,它会返回返回的数组。这样它就会连接外部数组,并将内容(内部数组)放在下一个索引处。
...otherwise for some reason it concats the Array being returned. This way it concats the outer Array, and placing the content (the inner Array) at the next index.
这篇关于如何在对象数组上使用jQuery.map()来返回数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文