0

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?

0

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

@ChanYoongHon glad to help. You can tick mark the answer if it was helpful to you
0

You can filter the keys:

const obj = {
    "mon": true,
    "tues": false,
    "wed": false,
    "thurs": true,
    "fri": false,
    "sat": true,
    "sun": false,
};
const options = Object.keys(obj).filter(key => obj[key]);

console.log(options);

Comments

0

Just another way with entries and array destructure

let day = {
  "mon": true,
  "tues": false,
  "wed": false,
  "thurs": true,
  "fri": false,
  "sat": true,
  "sun": false
};

let result = Object.entries(day).filter(([key, state]) =>state).map(([key]) => key);

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.