微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何检查值是否未定义以便不将其添加为对象字段?

如何解决如何检查值是否未定义以便不将其添加为对象字段?

我正在开发一个 Angular 应用程序,我对使用 TypeScript 有以下疑问,这些问题与如何正确避免在值未定义的情况下将字段插入到对象中有关。

在我的代码中,我以这种方式创建了一个对象:

let aestheticEvaluation: AestheticEvaluation = {
    "altezza": aestheticEvaluationInfo.get('altezza').value,"peso": aestheticEvaluationInfo.get('peso').value,"phototype": aestheticEvaluationInfo.get('phototype').value.name,"photoaging": aestheticEvaluationInfo.get('photoaging').value.name,"comedoni": aestheticEvaluationInfo.get('comedoni').value.name,"varici": aestheticEvaluationInfo.get('varici').value.name,"rugheGlifiche": aestheticEvaluationInfo.get('rugheGlifiche').value.name,"accentuazioneDeiPigheNasogeniene": aestheticEvaluationInfo.get('accentuazioneDeiPigheNasogeniene').value.name,"gradoAgingCollo": aestheticEvaluationInfo.get('gradoAgingCollo').value.name,"occhiaie": aestheticEvaluationInfo.get('occhiaie').value.name,"borse": aestheticEvaluationInfo.get('borse').value.name,"ipotoniaTerzoInferiore": aestheticEvaluationInfo.get('ipotoniaTerzoInferiore').value.name,"cellulite": aestheticEvaluationInfo.get('cellulite').value.name,"cicatriciIpertroficheCheloidi": aestheticEvaluationInfo.get('cicatriciIpertroficheCheloidi').value,"luceDiWood": aestheticEvaluationInfo.get('luceDiWood').value,"notes": aestheticEvaluationInfo.get('notes').value,};

问题是某些字段可能具有未定义值。在这种情况下,该字段不必插入到我的 aestheticEvaluation 对象中。

例如,如果 aestheticEvaluationInfo.get('altezza').value 值为 undefined,则此 "altezza" 字段不是插入对象

我知道我可以使用 if 语句来检查每个字段的值是否为 null 并且在这种情况下避免插入对象但是这样代码会很多更多余。有没有办法直接进入我的对象定义?

解决方法

创建一个函数,在创建对象之前检查对象中的每个值,如下图所示。

// data structure of `aestheticEvaluationInfo` seems to be a map
let aestheticEvaluationInfo = new Map();
aestheticEvaluationInfo.set('altezza',{ value: { name: 'altezza'}});
aestheticEvaluationInfo.set('peso',{ value: { name: 'peso'}});
.
.
.
// rest of the fields
aestheticEvaluationInfo.set('notes',undefined);


function createObjWithValues(data) {
  let obj = {};
  for (const [key,value] of data.entries()) {
    if (value) obj[key] = value.value.name;
  }
  return obj;
}

console.log(createObjWithValues(aestheticEvaluationInfo));
,

所以我有两种解决方案,你可以选择更适合你的一种。

  1. 默认值而不是未定义,在这种情况下,您将值设置为默认值(即空字符串 '',或数字为零等)。
  public function scopePublished()
{
    return $this->where('published_at','<=',today())->orderBy('published_at','desc');
}
  1. 设置对象值然后最后清理对象(删除未定义的值):
const object = {
    key: anotherObject?.attribute || ''
};

如果我有什么误解,请告诉我。

,
  let aestheticEvaluation: AestheticEvaluation = {
   "altezza": aestheticEvaluationInfo.get('altezza').value,"peso": aestheticEvaluationInfo.get('peso').value,....  so on
  }


  for(let key in aestheticEvaluation)
  { 
   if(!aestheticEvaluation[key]) 
    delete aestheticEvaluation[key]
   }

    !aestheticEvaluation[key] = This will include all falsy values such as 
    null,undefined,''.
    If you just want to check for undefined value you can use 
   for(let key in aestheticEvaluation)
    { 
     if(typeof aestheticEvaluation[key] === "undefined") 
      delete aestheticEvaluation[key]
    }
,

这是一种与其他解决方案略有不同的方法,但允许您只声明每个条件一次,并快速更改获取值的方法,因为它只在一行上。

let aestheticEvaluationCriteria: string[] = [
  "altezza","peso","phototype","photoaging","comedoni","varici","rugheGlifiche","accentuazioneDeiPigheNasogeniene","gradoAgingCollo","occhiaie","borse","ipotoniaTerzoInferiore","cellulite","cicatriciIpertroficheCheloidi","luceDiWood","notes",]

let aestheticEvaluation: AestheticEvaluation = {}
aestheticEvaluationCriteria.forEach((criteria)=>{
  const value = aestheticEvaluationInfo.get(criteria).value
  if (value){
    aestheticEvaluation[criteria] = value
  }
})
//aestheticEvaluation is now filled as specified
console.log(aestheticEvaluation)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。