假设我有一个这样的数组:
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都是可选项。