說我有一個這樣的數組:打破對象到基於的屬性單獨的數組的數組
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都是使用的選項。
感謝您的真正有益的迴應。改變了等號。 – Evan