英文:
Put if condition within find method for 0th element
问题
我有一个带有键的对象
```js
const People = {
groupA: ['Amy', 'Beth'],
groupB: ['Ben', 'Matt'],
groupC: ['Jen', 'Eli'],
};
现在我正在声明一个变量,我将在其中动态传递值。例如
const Person = Object.entries(People).find(([, values]) => values.includes('Amy'))[0];
console.log(Person); //输出 'groupA'
但是,当我传递的内容不在对象中时,它会抛出未定义错误。我无法理解在哪里放置 if 条件。
const Person1 = Object.entries(People).find(([, values]) => values.includes('Brad'))[0];
console.log(Person1); //输出应该是 undefined
我尝试过加入 if 条件,但无法处理两种情况。
<details>
<summary>英文:</summary>
I have an object with keys
const People = {
groupA : ['Amy','Beth'],
groupB : ['Ben','Matt'],
groupC : ['Jen','Eli'],
};
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'
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
I tried putting if conditions but was not able to handle both scenerios.
</details>
# 答案1
**得分**: 0
尝试使用[Nullish 合并运算符][1],这样你可以返回默认值,如果它是未定义的,你可以将其放在 '=>' 后的返回语句中。
或者在整个Object.entries语句之后的变量中,这取决于实际返回的是未定义的什么。
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
<details>
<summary>英文:</summary>
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 ‘=>’
Or after the entire Object.entries statement in the variable depending on what is actually returning undefined.
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
</details>
# 答案2
**得分**: 0
这是由于函数 `Array.prototype.find` 未找到条目,此函数在没有元素满足处理程序条件时返回 `undefined`。
另一种方法是使用 OR(`||`)运算符,如下所示,该方法将在函数 `find` 返回 `undefined` 时返回一个空数组。
在以下代码片段中,您可以看到其他备选方案。
```javascript
const Person = (Object.entries(People).find(([,values]) => values.includes('Amy')) || [])[0]
const People = { groupA : ['Amy','Beth'], groupB : ['Ben','Matt'], groupC : ['Jen','Eli']};
//OR运算符
let Person = (Object.entries(People).find(([,values]) => values.includes('Ele')) || [])[0];
console.log(Person);
//可选链运算符 (?.)
Person = Object.entries(People).find(([,values]) => values.includes('Ele'))?.[0];
console.log(Person);
//空值合并运算符
Person = (Object.entries(People).find(([,values]) => values.includes('Ele')) ?? [])[0];
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.
const Person = (Object.entries(People).find(([,values])=>values.includes('Amy')) || [])[0]
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const People = { groupA : ['Amy','Beth'], groupB : ['Ben','Matt'], groupC : ['Jen','Eli']};
//OR operator
let Person = (Object.entries(People).find(([,values])=>values.includes('Ele')) || [])[0];
console.log(Person);
//Optional chaining operator (?.)
Person = Object.entries(People).find(([,values])=>values.includes('Ele'))?.[0];
console.log(Person);
//Nullish coalescing operator
Person = (Object.entries(People).find(([,values])=>values.includes('Ele')) ?? [])[0];
console.log(Person);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论