簡體   English   中英

在LODASH中,斷言對象或數組中密鑰是否存在的最佳方法是什么?

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

在我的項目中,我可以收到兩種不同的響應:

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');
}

您可以使用lodash#some遍歷對象和數組來檢查某個鍵是否存在於任何級別。 只要確保將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> 

另一種方法是在使用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> 

這是遞歸檢查對象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; } 

注意:這不檢查數組中的鍵,僅檢查對象中的鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM