Skip to content Skip to sidebar Skip to footer

Use Object Type Output In Javascript Function

I created this function in my application: I want to change the function above into something like this: How to change the last function, because now i get undefined?

Solution 1:

There is no item in OUTPUT with the key seconds. The keys of the objects are conditions that become true or false.

So if you want to get the first item return the item that has the key 'true' if there is one, else return the item that has the key 'false'.

Working example:

consttest = (seconds) => {
  let dateType = "seconds";
  let time = seconds;

  constOUTPUT = {
    [seconds % 60 === 0]: {
      dateType: "minutes",
      time: seconds / 60,
    },
    [seconds % 3600 === 0]: {
      dateType: "hours",
      time: seconds / 3600,
    },
    [seconds % 86400 === 0]: {
      dateType: "days",
      time: seconds / 86400,
    },
  };

  console.log('OUTPUT: ', OUTPUT);
  
  returnOUTPUT['true'] ? OUTPUT['true'] : OUTPUT['false'];
};

console.log('60: ', test(60));
console.log('61: ', test(61));
console.log('3600: ', test(3600));
console.log('86400: ', test(86400));

Post a Comment for "Use Object Type Output In Javascript Function"