2017-09-27 41 views
0

篩選我們有對象的數組,這樣Loadash:從數組對象

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,該對象的列表,衝突對象在其正常價值?

回答

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>