在find方法中为第一个元素添加if条件。

huangapple go评论108阅读模式
英文:

Put if condition within find method for 0th element

问题

  1. 我有一个带有键的对象
  2. ```js
  3. const People = {
  4. groupA: ['Amy', 'Beth'],
  5. groupB: ['Ben', 'Matt'],
  6. groupC: ['Jen', 'Eli'],
  7. };

现在我正在声明一个变量,我将在其中动态传递值。例如

  1. const Person = Object.entries(People).find(([, values]) => values.includes('Amy'))[0];
  2. console.log(Person); //输出 'groupA'

但是,当我传递的内容不在对象中时,它会抛出未定义错误。我无法理解在哪里放置 if 条件。

  1. const Person1 = Object.entries(People).find(([, values]) => values.includes('Brad'))[0];
  2. console.log(Person1); //输出应该是 undefined

我尝试过加入 if 条件,但无法处理两种情况。

  1. <details>
  2. <summary>英文:</summary>
  3. I have an object with keys

const People = {
groupA : ['Amy','Beth'],
groupB : ['Ben','Matt'],
groupC : ['Jen','Eli'],
};

  1. Now I am declaring a varieable where I will pass the values dynamical. for example

const Person = Object.entries(People).find(([,values])=>values.includes('Amy'))[0]
console.log(Person) //output 'groupA'

  1. But when In pass something thats not in the object, its throwing an undefined error. I was not able to understand where to put the if condition.

const Person1 = Object.entries(People).find(([,values])=>values.includes('Brad'))[0]
console.log(Person1) // output should be undefined

  1. I tried putting if conditions but was not able to handle both scenerios.
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 尝试使用[Nullish 合并运算符][1],这样你可以返回默认值,如果它是未定义的,你可以将其放在 '=&gt;' 后的返回语句中。
  6. 或者在整个Object.entries语句之后的变量中,这取决于实际返回的是未定义的什么。
  7. [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
  8. <details>
  9. <summary>英文:</summary>
  10. Try using the [Nullish Coalescing Operator][1], this way you can returns. Default value if it is undefined, you would put this in the return statement after the ‘=&gt;’
  11. Or after the entire Object.entries statement in the variable depending on what is actually returning undefined.
  12. [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
  13. </details>
  14. # 答案2
  15. **得分**: 0
  16. 这是由于函数 `Array.prototype.find` 未找到条目,此函数在没有元素满足处理程序条件时返回 `undefined`
  17. 另一种方法是使用 OR`||`)运算符,如下所示,该方法将在函数 `find` 返回 `undefined` 时返回一个空数组。
  18. 在以下代码片段中,您可以看到其他备选方案。
  19. ```javascript
  20. const Person = (Object.entries(People).find(([,values]) => values.includes('Amy')) || [])[0]
  1. const People = { groupA : ['Amy','Beth'], groupB : ['Ben','Matt'], groupC : ['Jen','Eli']};
  2. //OR运算符
  3. let Person = (Object.entries(People).find(([,values]) => values.includes('Ele')) || [])[0];
  4. console.log(Person);
  5. //可选链运算符 (?.)
  6. Person = Object.entries(People).find(([,values]) => values.includes('Ele'))?.[0];
  7. console.log(Person);
  8. //空值合并运算符
  9. Person = (Object.entries(People).find(([,values]) => values.includes('Ele')) ?? [])[0];
  10. console.log(Person);
英文:

This is due to a not found entry by the function Array.prototype.find this function returns undefined when no element satisfies the handler condition.

An alternative is using the OR (||) operator as follows, this approach will return an empty array when the function find returns undefined.

Within the following code snippet, you can see additional alternatives.

  1. const Person = (Object.entries(People).find(([,values])=&gt;values.includes(&#39;Amy&#39;)) || [])[0]

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const People = { groupA : [&#39;Amy&#39;,&#39;Beth&#39;], groupB : [&#39;Ben&#39;,&#39;Matt&#39;], groupC : [&#39;Jen&#39;,&#39;Eli&#39;]};
  2. //OR operator
  3. let Person = (Object.entries(People).find(([,values])=&gt;values.includes(&#39;Ele&#39;)) || [])[0];
  4. console.log(Person);
  5. //Optional chaining operator (?.)
  6. Person = Object.entries(People).find(([,values])=&gt;values.includes(&#39;Ele&#39;))?.[0];
  7. console.log(Person);
  8. //Nullish coalescing operator
  9. Person = (Object.entries(People).find(([,values])=&gt;values.includes(&#39;Ele&#39;)) ?? [])[0];
  10. console.log(Person);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月28日 06:41:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定