gpt4 book ai didi

javascript - reactjs中如何使用条件运算符

转载 作者:行者123 更新时间:2023-12-03 17:26:35 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Is there a "null coalescing" operator in JavaScript?

(17 个回答)


2年前关闭。




在许多地方,我设置由等修改,由等创建:

const test = this.state.isValueEdited
? {
modifiedById: getLoggedInUser().userId,
}
: {
// TODO
};

我如何检查 getLoggedInUser有一些值(value),而不是访问 .userId .. 否则让我们设置 null .

谢谢

干杯

最佳答案

也使用 ternary operator

 modifiedById: getLoggedInUser() ? getLoggedInUser().userId : null

避免执行两次
const evaluation = getLoggedInUser()

{modifiedById: evaluation ? evaluation.userId : null}

关于javascript - reactjs中如何使用条件运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57793095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com