简体   繁体   English

在LODASH中,断言对象或数组中密钥是否存在的最佳方法是什么?

[英]In LODASH, What is the best way to assert the existence of a key inside an object or array?

In my project I can receive 2 different kinds of response: 在我的项目中,我可以收到两种不同的响应:

var data ={
  'ADULT':{},
  'bounds':[],
  'checkInEligible':true,
  'departureDate':'2016-07-15',
  'id':'car'
}

or I can get also the response: 或者我也可以得到响应:

var data = {
  ADULT:{
    'confirmationNumber':'29YNK',
    'checkInEligible':true
  },
  bounds:[],
  departureDate:"2016-07-15",`enter code here`
  id:"air"
}

I need to assert with some lodash method that the key 'checkInEligible' exist at some level into the response. 我需要使用一些lodash方法来断言键“ checkInEligible”在某种程度上存在于响应中。

I've tried with the method .some, but seems to work fine only with arrays and only for root level, because return true only when the object os at a root level. 我尝试使用.some方法,但似乎仅对数组有效并且仅对根级别有效,因为仅当对象os在根级别上才返回true。

I've tried this: 我已经试过了:

isCheckInEligible: function () {
    return _.some(data, function (value, key) {
        return key === 'checkInEligible' && value
    });
}

Or this: 或这个:

isCheckInEligible: function () {
    return _.some(data, 'checkInEligible');
}

Can somebody help me with this lodash method or any other lodash method? 有人可以用这种lodash方法或任何其他lodash方法帮助我吗? But I HAVE TO use lodash. 但是我必须使用lodash。

Thanks! 谢谢!

Maybe using _.has ? 也许使用_.has吗?

isCheckInEligible: () => {
    return _.has(data, 'checkInEligible') || _.has(data, 'ADULT.checkInEligible');
}

You can use lodash#some to traverse both objects and arrays to check if a certain key exists in any level. 您可以使用lodash#some遍历对象和数组来检查某个键是否存在于任何级别。 Just make sure to name the lodash#some iteratee to recursively traverse the object/array. 只要确保将lodash#some迭代器命名为递归地遍历对象/数组即可。

function hasKeyDeep(object, key) {
  return _.has(object, key) || _.isObject(object) &&
    _.some(object, _.partial(hasKeyDeep, _, key));
}

 function hasKeyDeep(object, key) { return _.has(object, key) || _.isObject(object) && _.some(object, _.partial(hasKeyDeep, _, key)); } console.log(hasKeyDeep({ 'ADULT':{}, 'bounds':[], 'checkInEligible':true, 'departureDate':'2016-07-15', 'id':'car' }, 'checkInEligible')); // => true console.log(hasKeyDeep({ 'ADULT':{}, 'bounds':[], 'departureDate':'2016-07-15', 'id':'car' }, 'checkInEligible')); // => false console.log(hasKeyDeep({ ADULT:{ 'confirmationNumber':'29YNK', 'checkInEligible':true }, bounds:[], departureDate:"2016-07-15", id:"air" }, 'checkInEligible')); // => true console.log(hasKeyDeep({ ADULT:{ 'confirmationNumber':'29YNK' }, bounds:[], departureDate:"2016-07-15", id:"air" }, 'checkInEligible')); // => false console.log(hasKeyDeep({ bounds:[{ 'checkInEligible': true, 'otherValue': 'another value' }] }, 'checkInEligible')); // => true console.log(hasKeyDeep({ bounds:[{ 'otherValue': 'another value' }] }, 'checkInEligible')); // => false console.log(hasKeyDeep({ a: [{ b: [{ c: [{ d: [{ 'checkInEligible': true }] }] }] }] }, 'checkInEligible')); // => true console.log(hasKeyDeep({ a: [{ b: [{ c: [{ d: [{ 'anotherKey': true }] }] }] }] }, 'checkInEligible')); // => false 
 body > div { top: 0; min-height: 100%; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

Another approach is to use RegExp#test in a stringified object using JSON#stringify . 另一种方法是在使用JSON#stringify的字符串化对象中使用RegExp#test

function hasKeyDeep(object, key) {
  return new RegExp('[{,]"' + key + '":')
    .test(JSON.stringify(object));
}

 function hasKeyDeep(object, key) { return new RegExp('[{,]"' + key + '":') .test(JSON.stringify(object)); } console.log(hasKeyDeep({ 'ADULT':{}, 'bounds':[], 'checkInEligible':true, 'departureDate':'2016-07-15', 'id':'car' }, 'checkInEligible')); // => true console.log(hasKeyDeep({ 'ADULT':{}, 'bounds':[], 'departureDate':'2016-07-15', 'id':'car' }, 'checkInEligible')); // => false console.log(hasKeyDeep({ ADULT:{ 'confirmationNumber':'29YNK', 'checkInEligible':true }, bounds:[], departureDate:"2016-07-15", id:"air" }, 'checkInEligible')); // => true console.log(hasKeyDeep({ ADULT:{ 'confirmationNumber':'29YNK' }, bounds:[], departureDate:"2016-07-15", id:"air" }, 'checkInEligible')); // => false console.log(hasKeyDeep({ bounds:[{ 'checkInEligible': true, 'otherValue': 'another value' }] }, 'checkInEligible')); // => true console.log(hasKeyDeep({ bounds:[{ 'otherValue': 'another value' }] }, 'checkInEligible')); // => false console.log(hasKeyDeep({ a: [{ b: [{ c: [{ d: [{ 'checkInEligible': true }] }] }] }] }, 'checkInEligible')); // => true console.log(hasKeyDeep({ a: [{ b: [{ c: [{ d: [{ 'anotherKey': true }] }] }] }] }, 'checkInEligible')); // => false 
 body > div { top: 0; min-height: 100%; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

Here is a recursive check down the object has-a tree to see if any contained object has the attribute (object property) that you're looking for. 这是递归检查对象has-ahas-a棵树,以查看是否有任何包含的对象具有您要查找的属性(对象属性)。

 function nestedHas(object, attrib) { if (_.has(object, attrib)) { return true; } for (let key in _.pickBy(object, _.isObject)) { if (nestedHas(object[key], attrib)) { return true; } } return false; } 

Note: This doesn't check for the key in arrays, just in objects. 注意:这不检查数组中的键,仅检查对象中的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM