Search code examples
javascriptpipejavascript-objectsdefault-value

Is it possible to use pipe (||) to set default parameters inside JavaScript object value


const empiUpdateObject = {
          "parameters": {
            "bloodGroup": bloodCodeArray
              .map(bg => bg.code)
              .filter(bc => bc.toUpperCase() === bloodType.toUpperCase())
              .pop() || bloodType
          }
        }

If the defined array did not retrieve can I use '||' to set a default value to the object property


Solution

  • function setValue(arg){
        console.log(arg);
        const obj = {
            name: arg || 'default'
        }
        console.log(obj);
    }
    
    setValue('Pointy');
    

    Please note this solution only works as long as false or falsy values are not required to be displayed. And the solution will only show true / truthy values.