简体   繁体   English

带有三元运算符的 Javascript 对象声明

[英]Javascript object declaration with ternary operator

I'm declaring an object inside one of my components like below:我在我的组件之一中声明一个对象,如下所示:

const data = {
        user: id,
        userRole: role,
        teacherdepartment: department
}

But now I wanted to do this declaration dynamically depends on a specific value, like below:但是现在我想根据特定值动态执行此声明,如下所示:

let usertype='teacher';
const data = {
        user: id,
        userRole: role,
        if(usertype=='teacher'?(teacherdepartment:tdepartment):(studentDepartment:sdepartment))
}

Is this possible.这可能吗。 I know I can do it with nested ternary operator.我知道我可以用嵌套的三元运算符来做到这一点。 But inside the object structure any simple line that can do that trick?但是在对象结构内部,任何简单的线条都可以做到这一点?

Update: object values can be easily set using ternary inside the object declaration, but this is for object key so this is not a duplicate of this .更新:对象的值可以使用对象的声明中三元轻松设定,但这是对象键,这不是重复这个 Also, in the above example I have put a simple object.另外,在上面的例子中,我放了一个简单的对象。 Image if the objects have some child and ternary conditions within.如果对象中有一些子条件和三元条件,则为图像。

I'd avoid a ternary operator altogether because they're confusing to read in a lot of situations.我会完全避免使用三元运算符,因为它们在很多情况下阅读起来很混乱。 Instead I would create a dictionary that maps user types to string values, and then create the property dynamically with that information.相反,我会创建一个将用户类型映射到字符串值的字典,然后使用该信息动态创建属性。

 const userType = 'teacher'; const dict = { teacher: 'tdepartment', student: 'sdepartment' }; const data = { user: 'id', userRole: 'role', [`${userType}Department`]: dict[userType] } console.log(data);

Try with conditional operator for both key and value.尝试对键和值使用条件运算符。 Keys can be made dynamic by adding [] around the key expression.通过在键表达式周围添加[]可以使键变得动态。

Pseude Code伪代码

const data = {
    user: id,
    userRole: role,
    [usertype=='teacher'? 'teacherdepartment' : 'studentDepartment']: usertype=='teacher'? tdepartment: sdepartment,
}

Working Code工作代码

 const usertype = 'student'; const tdepartment = 'tdepartment'; const sdepartment = 'sdepartment'; const id = 'id'; const role = 'role'; const data = { user: id, userRole: role, [usertype=='teacher'? 'teacherdepartment' : 'studentDepartment']: usertype=='teacher'? tdepartment: sdepartment, }; console.log(data)

I think this could be refactored into我认为这可以重构为

 let usertype = 'teacher'; let departmentProperty = usertype === 'teacher' ? 'teacherdepartment' : 'studentDepartment'; let departmentValue = usertype === 'teacher' ? 'teacherdepartmentValue' : 'studentDepartment'; const data = { user: 'id', userRole: 'role', [departmentProperty]: departmentValue, }; console.log(data)

While this can be done in a "one-liner" IMO to preserve readability it shouldn't be.虽然这可以在“单行”IMO 中完成以保持可读性,但它不应该如此。

Instead, check usertype and create an object to include in the resulting data object.相反,检查用户usertype并创建一个对象以包含在结果data对象中。 This way the changes based on usertype are isolated and easy to reason about.这样基于用户usertype的更改是隔离的并且易于推理。 It also allows for additional changes based on usertype as it's isolated from the static properties.它还允许基于用户usertype进行其他更改,因为它与静态属性隔离。

const deptInfo = usertype === 'teacher' ? { teacherDepartment: tDepartment }
  : { studentDepartment: sDepartment }

const data = {
  ...deptInfo,
  user: id,
  userRole: role,
}

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

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