假设我有一个这样的数组:
var arr = [
{type:"orange", title:"First"},
{type:"orange", title:"Second"},
{type:"banana", title:"Third"},
{type:"banana", title:"Fourth"}
];
我希望将其拆分为具有相同类型对象的数组,因此:
[{type:"orange", title:"First"},
{type:"orange", title:"Second"}]
[{type:"banana", title:"Third"},
{type:"banana", title:"Fourth"}]
但我想一般地这样做,所以没有指定橙色或香蕉的 if 语句
// not like this
for (prop in arr){
if (arr[prop] === "banana"){
//add to new array
}
}
想法?JQuery 和 Underscore 都是可以使用的选项。