2

I am working on an Angular application and I have the following doubt using TypeScript related how to correctly avoid to insert a field into an object if the value is undefined.

In my code I am creating an object in this way:

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,
};

The problem is that some fields can have undefined value. In this case the field have not to be inserted into my aestheticEvaluation object.

So for example, if the aestheticEvaluationInfo.get('altezza').value value is undefined this "altezza" field is not inserted in the object

I know that I can use an if statment in order to check if the value of each fields is null and in this case avoid to insert the object but in this way the code will be a lot more redundant. Exist a way to do directly into my object definition?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 1
    Does this answer your question? [Remove blank attributes from an Object in Javascript](https://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript) – futur Jun 19 '21 at 17:29
  • I don't believe there's a way to do it inline while defining the object. – futur Jun 19 '21 at 17:29
  • https://stackoverflow.com/questions/46957194/javascript-es6-spread-operator-on-undefined – The Fool Jun 19 '21 at 17:30
  • how about `let foo = {...a.value ? {val : a.value} : {}}` ? – The Fool Jun 19 '21 at 17:32

4 Answers4

1

Create a function that checks for each value in the object before creating it as illustrated below.

// 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));
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
1

So I have two solutions in my mind you may choose the one the suits you better.

  1. Default value instead of undefined, in this case, you set the value as the default value (ie. empty string '', or zero for numbers, etc..).
const object = {
    key: anotherObject?.attribute || ''
};
  1. Set the object values then clean the object at last (remove undefined values):
const object = { key: value };

Object.keys( object ).forEach( key => {
    if ( !object[key] ) delete object[key];
} );

Please let me know if I'm misunderstanding anything.

1
  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,0,''.
    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]
    }
Nikita
  • 11
  • 2
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Jun 19 '21 at 21:23
1

Here's a way that's a bit different than the other solutions, but allows you to only declare each criteria once, and quickly change out the method of getting the values since it's only on one line.

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)
Donald Duck
  • 618
  • 2
  • 6