简体   繁体   中英

Vue JS lodash findKey nested object with dot notation returns undefined

I'm trying to pull out a value for a nested object key from some eligibility array that I've got, but I'm getting an undefined value for some reason and need to know what I'm missing.

Given the following array:

const eligibilityCriteria = [
  { field: 'loan.amount', operator: '>=', value: 1000 },
  { field: 'loan.term', operator: '>=', value: 1 },
  { field: 'applicant.birthday', operator: '>=', value: 40 },
  { field: 'applicant.isHomeowner', operator: '==', value: false }
]

I need to find loan.amount from a nested object and pull out it's value:

My big nested object is (coming from the store)

application: {
  meta: {
    brand: '',
    version: '',
    affiliate: '',
    affiliate_full: '',
    campaign: '',
    client_hostname: '',
    client_href: '',
    client_origin: '',
    client_useragent: '',
    client_ip: '127.0.0.1',
    icicle_hash: ''
  },
  loan: {
    amount: 500,
    term: null,
    purpose: null
  }
}

My function right now is:

checkEligibility () {
  const eligibilityCriteria = [
    { field: 'loan.amount', operator: '>=', value: 1000 },
    { field: 'loan.term', operator: '>=', value: 1 },
    { field: 'applicant.birthday', operator: '>=', value: 40 },
    { field: 'applicant.isHomeowner', operator: '==', value: false }
  ]

  for (const [index, offer] of this.datasets.offers.entries()) {
    const eligibility = eligibilityCriteria

    if (eligibility) {
      for (const [ci, criteria] of eligibility.entries()) {

        // TODO: this fails to pull the value, returns undefined
        const field = _.findKey(this.$store.state.application.application, criteria.field)
      }
    }
  }
}

What am I missing?

You must have to misunderstood what _.findKey() does

This method is like _.find except that it returns the key of the first element predicate returns truthy for instead of the element itself.

See Example 2 in the code below.

If you want to retrieve the value from the object at a given path, you must use _.property() (Example 2 below)

 const eligibilityCriteria = [ { field: 'loan.amount', operator: '>=', value: 1000 }, { field: 'loan.term', operator: '>=', value: 1 }, ] const application = { loan: { amount: 500, term: 2, amount2: 0, } } // example 1 // outputs "loan" console.log(_.findKey(application, "amount")) // outputs undefined - there is no key in application object that has property chain "loan.amount" console.log(_.findKey(application, "loan.amount")) // outputs undefined - "amount2" key is there but the value is falsy console.log(_.findKey(application, "amount2")) // example 2 for (const [ci, criteria] of eligibilityCriteria.entries()) { console.log(criteria.field, _.property(criteria.field)(application)) }
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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