【发布时间】:2013-01-19 17:14:30
【问题描述】:
假设我有一个这样的数组:
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 都是可以使用的选项。
【问题讨论】:
-
感谢您提供非常有用的回复。更改了等号。
标签: javascript jquery arrays object underscore.js