arraysangulartypescriptboolean

Convert Boolean Value with true into an array in Angular4 (Typescript)


I am getting a set of boolean values from my microservice.

   "mon": true,
   "tues": false,
   "wed": false,
   "thurs": true,
   "fri": false,
   "sat": true,
   "sun": false,

And I need to convert the values with boolean value true to an array like following:

options = ['mon', 'thurs', 'sat']

How can I do it?


Solution

  • You need to use filter on the object keys for those days key:

    var day = {
      "mon": true,
      "tues": false,
      "wed": false,
      "thurs": true,
      "fri": false,
      "sat": true,
      "sun": false
    };
    
    var res = Object.keys(day).filter(key => day[key]);
    console.log(res);