简体   繁体   English

如何在对象数组上使用jQuery.map()来返回数组数组

[英]How to use jQuery.map() on array of objects to return array of arrays

I would like to use jQuery to convert an array of objects to array of arrays using map. 我想使用jQuery将对象数组转换为使用map的数组数组。

For example if I have this: 例如,如果我有这个:

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 ];
});

So that the result would be: 结果将是:

ArrArr = [[1,2],[2,3],[3,4]]

With the jQuery.map() (docs) and map() (docs) methods you need to double wrap the return value: 使用jQuery.map() (docs)map() (docs)方法,您需要双重包装返回值:

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. 这样它就会连接外部数组,并将内容(内部数组)放在下一个索引处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM