2016-07-10 12 views
1

den folgenden JS-Array Geben Sie:Wie Objekt mit dem Wert in dem Feld finden mit lodash

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" 
     }, 
    }, 
    ] 

an einem Punkt in einer Schleife, habe ich die station.id und Daypart (am oder pm) Werte, und ich müssen sehen, ob das todayShift-Array ein Objekt enthält, das sich im entsprechenden dayPart befindet und den Wert stations.id hat, und dieses Objekt zurückgeben, wenn es existiert. Ich habe schon versucht, diese mit lodash:

 if (typeof vm.todayShifts[dayPart] != 'undefined') { 
     var shift = _.find(vm.todayShifts[dayPart], {'station': station.id}); 
     } 

aber nichts zurückgegeben, auch wenn es Daten gibt, die die Kriterien entsprechen (z Daypart = „am“ und Station = 1).

Dies ist bereits in einer Schleife (innerhalb einer cell modifier für eine custom cell template mit Angular Bootstrap-Kalender), so dass ich nicht jeden Tag Schleifen möchte jedes Mal, wenn ich nicht muss, da dieser Controller wird aufgerufen ~ 30 Mal pro Seite.

Bin ich nah dran? Oder gibt es eine einfachere Möglichkeit, nach diesem Objekt zu suchen und es zu bekommen?

Danke.

+1

Warum 'ist ein Array todayShifts', wenn Sie es wie ein Objekt behandeln. Ich weiß, Javascript ermöglicht Ihnen, seltsame Dinge wie das zu tun, aber warum ... – Damon

Antwort

0

Verwendung eine Funktion statt

var shift = _.find(vm.todayShifts[dayPart], function(shift){ return shift.station == station.id }); 
+0

Die zweite Option funktioniert perfekt. Vielen Dank. – wonder95

1

Blick in die Konsole für das Ergebnis.

vm = Object; 
 
vm.todayShifts = []; 
 
vm.todayShifts['am'] = [ 
 
    { 
 
     station: "1", 
 
     slots: [{ 
 
\t   position: "AO", 
 
\t   name: "Person One" 
 
\t  }, 
 
\t  { 
 
\t   position: "FF", 
 
\t   name: "Person Two" 
 
\t  }, 
 
\t  { 
 
\t   position: "PFF", 
 
\t   name: "Person Three" 
 
\t  }] 
 
    }, 
 
    { 
 
     station: "2", 
 
     slots: [{ 
 
\t   position: "AO", 
 
\t   name: "Person Four" 
 
\t  }, 
 
\t  { 
 
\t   position: "FF", 
 
\t   name: "Person Fve" 
 
\t  }, 
 
\t  { 
 
\t   position: "PFF", 
 
\t   name: "Person Six" 
 
\t  }] 
 
    }, 
 
    ], 
 
vm.todayShifts['pm'] = [ 
 
    { 
 
     station: "1", 
 
     slots: [{ 
 
\t   position: "AO", 
 
\t   name: "Person Seven" 
 
\t  }, 
 
\t  { 
 
\t   position: "FF", 
 
\t   name: "Person Eight" 
 
\t  }, 
 
\t  { 
 
\t   position: "PFF", 
 
\t   name: "Person Nine" 
 
\t  }] 
 
    }, 
 
    { 
 
     station: "2", 
 
     slots: [{ 
 
\t   position: "AO", 
 
\t   name: "Person Ten" 
 
\t  }, 
 
\t  { 
 
\t   position: "FF", 
 
\t   name: "Person Eleven" 
 
\t  }, 
 
\t  { 
 
\t   position: "PFF", 
 
\t   name: "Person Twelve" 
 
\t  }] 
 
    } 
 
    
 
    ] 
 
    
 
    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.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>