0
我想用underscore.js實現某種hasObject函數。查找數組中的對象
例子:
var Collection = {
this.items: [];
this.hasItem: function(item) {
return _.find(this.items, function(existingItem) { //returns undefined
return item % item.name == existingItem.name;
});
}
};
Collection.items.push({ name: "dev.pus", account: "stackoverflow" });
Collection.items.push({ name: "margarett", account: "facebook" });
Collection.items.push({ name: "george", account: "google" });
Collection.hasItem({ name: "dev.pus", account: "stackoverflow" }); // I know that the name would already be enough...
出於某種原因,強調尋找不確定的回報......
我在做什麼錯?
究竟是什麼,你認爲'%'操作符呢? – Pointy 2012-07-15 14:19:18
你說你想知道一個對象是否包含另一個對象,但是你似乎只是在判斷'name'屬性是否相同。那麼整個對象應該匹配還是僅僅是'name'屬性? – Utkanos 2012-07-15 14:20:39
@Utkanos就像你在評論中看到的那樣,它只是測試這個名字,但是知道如何測試整個對象會很好 – 2012-07-15 15:56:02