Give the following JS array:
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"
},
},
]
at one point in a loop, I have the station.id and dayPart (am or pm) values, and I need to see if the todayShift array contains an object that is in the appropriate dayPart and has the station.id value, and return that object if it exists. I've tried this with lodash:
if (typeof vm.todayShifts[dayPart] != 'undefined') {
var shift = _.find(vm.todayShifts[dayPart], {'station': station.id});
}
but nothing is returned even when there is data that matches the criteria (e.g. dayPart="am" and station = 1).
This is already in a loop (inside a cell modifier for a custom cell template with Angular Bootstrap calendar), so I don't want to loop through todayShifts every time if I don't have to, since this controller will get called ~30 times per page.
Am I close? Or is there an easier way to check for and get that object?
Thanks.
Give the following JS array:
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"
},
},
]
at one point in a loop, I have the station.id and dayPart (am or pm) values, and I need to see if the todayShift array contains an object that is in the appropriate dayPart and has the station.id value, and return that object if it exists. I've tried this with lodash:
if (typeof vm.todayShifts[dayPart] != 'undefined') {
var shift = _.find(vm.todayShifts[dayPart], {'station': station.id});
}
but nothing is returned even when there is data that matches the criteria (e.g. dayPart="am" and station = 1).
This is already in a loop (inside a cell modifier for a custom cell template with Angular Bootstrap calendar), so I don't want to loop through todayShifts every time if I don't have to, since this controller will get called ~30 times per page.
Am I close? Or is there an easier way to check for and get that object?
Thanks.
Share Improve this question asked Jul 10, 2016 at 5:06 wonder95wonder95 4,3158 gold badges50 silver badges82 bronze badges 1-
1
Why is
todayShifts
an array if you are treating it like an object. I know javascript allows you to do weird things like this, but why ... – Damon Commented Jul 10, 2016 at 5:21
2 Answers
Reset to default 1use a function instead
var shift = _.find(vm.todayShifts[dayPart], function(shift){ return shift.station == station.id });
look into console for the result.
vm = Object;
vm.todayShifts = [];
vm.todayShifts['am'] = [
{
station: "1",
slots: [{
position: "AO",
name: "Person One"
},
{
position: "FF",
name: "Person Two"
},
{
position: "PFF",
name: "Person Three"
}]
},
{
station: "2",
slots: [{
position: "AO",
name: "Person Four"
},
{
position: "FF",
name: "Person Fve"
},
{
position: "PFF",
name: "Person Six"
}]
},
],
vm.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"
}]
}
]
function getShift(dayPart, stationId){
if (typeof vm.todayShifts[dayPart] != 'undefined') {
var shift = _.filter(vm.todayShifts[dayPart], {'station': stationId});
return shift;
}
}
var ob = getShift("pm", "2");
console.log(ob);
<script src="https://cdnjs.cloudflare./ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>