简体   繁体   English

带有点符号的Vue JS lodash findKey嵌套object返回未定义

[英]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.我正在尝试从我拥有的某个资格数组中提取嵌套 object 键的值,但由于某种原因我得到了一个未定义的值,并且需要知道我缺少什么。

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:我需要从嵌套的 object 中找到loan.amount并提取它的值:

My big nested object is (coming from the store)我的大嵌套 object 是(来自商店)

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:我现在的 function 是:

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您必须误解_.findKey()的作用

This method is like _.find except that it returns the key of the first element predicate returns truthy for instead of the element itself.此方法与 _.find 类似,只是它返回第一个元素谓词的键,而不是元素本身返回真值。

See Example 2 in the code below.请参见下面代码中的示例 2

If you want to retrieve the value from the object at a given path, you must use _.property() (Example 2 below)如果要从给定路径的 object 中检索值,则必须使用_.property() (下面的示例 2)

 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>

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

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