【发布时间】:2012-05-23 10:49:03
【问题描述】:
我使用 jQuery 和 underscore.js,我有 title1-2,我希望有相应的操作。
this.items = {
menuItems: [
{ title: 'title1',
data: [
{ title: 'title1-1',
action: 'action1-1'
},
{ title: 'title1-2',
action: 'action1-2'
}
]
},
{ title: 'title2',
data: [
{ title: 'title2-1',
action: 'action2-1'
},
{ title: 'title2-2',
action: 'action2-2'
}
]
}
]
};
目前,我有以下代码来执行此操作:
var item = _.find(_.flatten(_.pluck(this.items.menuItems, 'data')), function (item) { return item.title === 'title1-2'; });
console.log(item.action);
有没有更好的方法找到它?
【问题讨论】:
-
对我来说看起来还不错。不过这个问题可能更适合codereview.stackexchange.com
标签: javascript jquery arrays underscore.js