Object to array

TypeScript
Easy

Objective

Write a function that takes an object, and returns an array of strings

Requirements

  1. The function should be called objectToArray

  2. It should take a single parameter, which is an object

    1. The object may have any number of key-value pairs

    2. The values will be boolean

  3. The function should return an array of strings

  4. The returned array should only contain keys from the object parameter, where the corresponding value is true

  5. If the object is empty, or no values are true: the function should return an empty array

  6. You should use TypeScript

Check your solution

Tests

Function name is objectToArray
objectToArray({item1:true,item2:false,item3:true})Expected:["item1","item3"]

Solution

Here's how I would approach this, but keep in mind there might be other ways to solve this problem.

function objectToArray(object: Record<string, boolean>): string[] { return Object.keys(object).filter(key => object[key]); }
Click to reveal