我有一组对象,见下文:
var orderbyColumns = [Object { Descending=true, TableName="Users", ColumnName="RankCode", more...}, Object { Descending=false, TableName="Users", ColumnName="LastName", more...}]
现在我想通过TableName和ColumnName属性从orderbyColumns中提取。
如果以下工作会很好,但事实并非如此。有什么想法吗?
var filter = $(orderbyColumns).find('[ColumnName=' + columnName + ']');
如果你可以这样做会很好:
var filter = from x in orderbyColumns where x.TableName == "Users" && x.ColumnName == "RankCode" select x;
答案 0 :(得分:3)
我可能会这样做:
var orderByColumns = [ { Descending:true, TableName:"Users", ColumnName:"RankCode", more:"..."}, { Descending:false, TableName:"Users", ColumnName:"LastName", more:"..."}]
orderByColumns.findObject = findObject;
var filter = orderByColumns.findObject("ColumnName","LastName");
alert(filter.Descending); // returns 'false'
function findObject(name,val) {
var len = this.length;
for (var i=0; i<len; i++) {
if (this[i][name]===val) { return this[i]; }
}
return null;
}
答案 1 :(得分:1)