如何使用lodash

时间:2016-07-10 05:06:03

标签: javascript arrays object lodash

提供以下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" and station = 1),也不会返回任何内容。

这已经在一个循环中(在cell modifier内有一个带有Angular Bootstrap日历的custom cell template),所以如果我不这样做的话,我不想每次循环播放。 t必须,因为这个控制器每页被调用~30次。

我关门了吗?或者有更简单的方法来检查和获取该对象吗?

感谢。

2 个答案:

答案 0 :(得分:1)

查看控制台的结果。



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.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用函数

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