【问题标题】:How to find object with value in array using lodash如何使用 lodash 在数组中查找具有值的对象
【发布时间】: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


【解决方案1】:

改用函数

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

【讨论】:

    【解决方案2】:

    查看控制台查看结果。

    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 2016-08-10
      相关资源
      最近更新 更多