我有一個對象數組。我想過濾它們,然後對過濾的對象執行一些操作。這是我迄今的嘗試。更新數組中的過濾對象
持倉對象
function Position (title, type, purchasable){
this.title = title;
this.type = type;
this.purchasable = purchasable || false;
}
function Purchasable (prices){
this.owner = "unowned";
this.rating = 0;
this.prices = prices;
this.price = this.prices[this.rating];
}
function City (title,set,prices){
Position.call(this, title, "city", true);
Purchasable.call(this, prices);
this.set = set;
}
positions = [
new City("Cairo", "brown", [60,2,10,30,90,160,250]),
new City("Vienna", "brown", [60,4,20,60,180,320,450]),
new City("Vienna", "blue", [60,4,20,60,180,320,450])
];
});
功能
function test() {
var set = "brown";
positions.filter(function(obj){
obj.set === "brown"; //do something to every brown set, eg owner = me
});
//I want to change the values of the objs in positions array
}
是有問題的地方,隱藏得很好? –
你可以顯示你的「城市」對象嗎? – Praveen
@praveen我編輯了我的問題以顯示City對象。 – Elton