【发布时间】:2016-07-10 05:06:03
【问题描述】:
给出如下JS数组:
vm.todayShifts = [];
vm.todayShifts['am'] =
{
station: "1",
slots: {
position: "AO",
name: "Person One"
},
slots: {
position: "FF",
name: "Person Two"
},
slots: {
position: "PFF",
name: "Person Three"
},
},
{
station: "2",
slots: {
position: "AO",
name: "Person Four"
},
slots: {
position: "FF",
name: "Person Fve"
},
slots: {
position: "PFF",
name: "Person Six"
},
},
],
todayShifts['pm'] =
{
station: "1",
slots: {
position: "AO",
name: "Person Seven"
},
{
position: "FF",
name: "Person Eight"
},
{
position: "PFF",
name: "Person Nine"
},
},
{
station: "2",
slots: {
position: "AO",
name: "Person Ten"
},
{
position: "FF",
name: "Person Eleven"
},
{
position: "PFF",
name: "Person Twelve"
},
},
]
在循环中的某一时刻,我有 station.id 和 dayPart(am 或 pm)值,我需要查看 todayShift 数组是否包含位于适当 dayPart 中且具有 station.id 值的对象,如果存在则返回该对象。我已经用 lodash 试过了:
if (typeof vm.todayShifts[dayPart] != 'undefined') {
var shift = _.find(vm.todayShifts[dayPart], {'station': station.id});
}
但即使存在符合条件的数据(例如 dayPart="am" 和 station = 1),也不会返回任何内容。
这已经在一个循环中(在 cell modifier 中,对于带有 Angular Bootstrap 日历的 custom cell template),所以我不想每次都循环遍历 todayShifts,因为这个控制器会每页被调用约 30 次。
我接近了吗?还是有更简单的方法来检查和获取该对象?
谢谢。
【问题讨论】:
-
如果您将
todayShifts视为一个对象,为什么它是一个数组。我知道 javascript 允许你做这样奇怪的事情,但是为什么......
标签: javascript arrays object lodash