简体   繁体   English

在对象字面量中使用带有三元运算符的展开语法

[英]using spread syntax with ternary operator inside object literal

Consider following code, where one object is composed from two parts: values and their corresponding descriptions:考虑以下代码,其中一个对象由两部分组成:值及其对应的描述:

let flagConfigObj = {};

const features = {
  f1: {
    value: 1,
    type: 'type1'
  },
  f2: {
    value: 2,
    type: 'type2'
  }
};

let featuresDescription = {
  f1: {
    description: 'desc 1'
  },
  f2: {
    description: 'desc 2'
  }
};

Object.keys(features).forEach(key => {
  flagConfigObj[key] = {
    ...features[key],
    ...(featuresDescription[key] || { some: 'backup' })
  };
});

console.log(flagConfigObj);

As you might notice, when composite is constructed, there is ||您可能会注意到,在构造复合时,有|| operator preventing form spreading non-existing object and replacing it with some default templates.运算符防止表单传播不存在的对象并将其替换为一些默认模板。 And so, output for this code will be:因此,此代码的输出将是:

{ 
  f1: { value: 1, type: 'type1', description: 'desc 1' },
  f2: { value: 2, type: 'type2', description: 'desc 2' } 
}

But when corresponding description field in featuresDescription is missing, it's replaced to:但是当featuresDescription对应的 description 字段丢失时,它会替换为:

{ 
  f1: { value: 1, type: 'type1', description: 'desc 1' },
  f2: { value: 2, type: 'type2', some: 'backup' } 
}

The question问题

How to replace ||如何更换|| syntax with ternary operator ?: ?三元运算符的语法?: ? I've tried some funny combinations like我尝试了一些有趣的组合,比如

featuresDescription[key] ? ...featuresDescription[key] : {some: 'backup'}
{featuresDescription[key]} ? ...featuresDescription[key] : {some: 'backup'}

But all of them gives syntax error, and I'm feeling now like just blindly randomizing positions of [,],},{,... instead of truly understanding it...但是它们都给出了语法错误,我现在感觉就像是盲目地随机化[,],},{,...而不是真正理解它...

Any help as always is appreciated!一如既往的任何帮助表示赞赏!

The spread syntax is part of the object initializer grammar but not part of the general expression grammar, so your entire ? :展开语法是对象初始值设定项语法的一部分,但不是通用表达式语法的一部分,所以您的整个? : ? : has to be to the right: ? :必须在右边:

 ... featuresDescription[key] ? featuresDescription[key] : { some: "backup" }

In this particular case, there's not any effective difference between this and the original with the simple ||在这种特殊情况下,这与原始的简单||之间没有任何有效区别. . The expression表达方式

expr1 ? expr1 : expr2

means, "if expr1 evaluates to a "truthy" value, use its value; otherwise use the value of expr2".意思是,“如果 expr1 评估为“真实”值,则使用其值;否则使用 expr2 的值”。 That is precisely the same as what这和什么完全一样

expr1 || expr2

means.方法。 (Well, ignoring possible side-effects of expr1 , which would be an even stronger incentive to using the simpler || form unless you're laying booby-traps for some future programmer.) (好吧,忽略expr1可能的副作用,这将是使用更简单的||形式的更强烈的动机,除非您为某些未来的程序员设置陷阱。)

I think this would work我认为这会奏效

...(featuresDescription[key] ? featuresDescription[key] : { some: 'backup' })

full example:完整示例:

 let flagConfigObj = {}; const features = { f1: { value: 1, type: 'type1' }, f2: { value: 2, type: 'type2' }, f3: { value: 3, type: 'type3' } }; let featuresDescription = { f1: { description: 'desc 1' }, f2: { description: 'desc 2' }, }; Object.keys(features).forEach(key => { flagConfigObj[key] = { ...features[key], ...(featuresDescription[key] ? featuresDescription[key] : { some: 'backup' }) }; }); console.log(flagConfigObj);

However, I still prefer using ||但是,我还是更喜欢使用|| as below如下

...(featuresDescription[key] || { some: 'backup' })

because it is simpler and not repetitive regarding featuresDescription[key]因为它更简单且不重复featuresDescription[key]

Hope it helps希望能帮助到你

I think this might be the syntax you are looking for;我认为这可能是您正在寻找的语法;

Object.keys(features).forEach(key => {
        flagConfigObj[key] = {
            ...features[key],
            ...(featuresDescription[key] ? featuresDescription[key] : {some: 'backup'})
    }
})

You could wrap the default expression in parenthese before spreading the object.您可以在扩展对象之前将默认表达式括在括号中。

 let flagConfigObj = {}; const features = { f1: { value: 1, type: 'type1' }, f2: { value: 2, type: 'type2' } }; let featuresDescription = { f1: { description: 'desc 1' }, f2: { description: 'desc 2' } }; Object.keys(features).forEach(key => { flagConfigObj[key] = { ...features[key], ...(featuresDescription[key] || { some: 'backup' }) } }) console.log(flagConfigObj)

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

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