在我的项目中,我可以收到两种不同的响应:
var data ={
'ADULT':{},
'bounds':[],
'checkInEligible':true,
'departureDate':'2016-07-15',
'id':'car'
}
或者我也可以得到响应:
var data = {
ADULT:{
'confirmationNumber':'29YNK',
'checkInEligible':true
},
bounds:[],
departureDate:"2016-07-15",`enter code here`
id:"air"
}
我需要使用一些 lodash 方法断言键“checkInEligible”在某个级别存在于响应中。
我已经尝试过 .some 方法,但似乎只适用于数组并且只适用于根级别,因为只有当对象 os 处于根级别时才返回 true。
我已经试过了:
isCheckInEligible: function () {
return _.some(data, function (value, key) {
return key === 'checkInEligible' && value
});
}
或者这个:
isCheckInEligible: function () {
return _.some(data, 'checkInEligible');
}
有人可以帮助我使用这个 lodash 方法或任何其他 lodash 方法吗? 但我必须使用 lodash。
谢谢!
最佳答案
也许使用 _.has
?
isCheckInEligible: () => {
return _.has(data, 'checkInEligible') || _.has(data, 'ADULT.checkInEligible');
}
关于javascript - 在 LODASH 中,断言对象或数组中存在键的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44209183/