Loadash:从数组对象中过滤

时间:2017-09-27 07:52:18

标签: lodash

我们有一个对象数组

var myArr = [ {name: "john", age:23, conflict:['booking', ' double booking']}
              {name: "john", age:43, conflict:['booking', ' double booking']}
              {name: "jim", age:101, conflict:['normal', ' double booking']}
              {name: "bob", age:67, conflict:['cancelled', ' double booking']} ];

如何从myArr获取对象列表,其中的冲突对象在其中具有正常值?

1 个答案:

答案 0 :(得分:0)

您可以使用filterfind来获取冲突数组中normal的对象。



var myArr = [ {name: "john", age:23, conflict:['booking', ' double booking']},{name: "john", age:43, conflict:['booking', ' double booking']},{name: "jim", age:101, conflict:['normal', ' double booking']},{name: "bob", age:67, conflict:['cancelled', ' double booking']} ],
    searchWord = 'normal';

var result = _.filter(myArr, function(o) { 
  return _.find(o.conflict, function(word) { 
    return word.toLowerCase() == searchWord.toLowerCase(); 
  }); 
});
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;