Object to array
TypeScript
Easy
Objective
Write a function that takes an object, and returns an array of strings
Requirements
The function should be called
objectToArrayIt should take a single parameter, which is an object
The object may have any number of key-value pairs
The values will be
boolean
The function should return an array of strings
The returned array should only contain keys from the object parameter, where the corresponding value is
trueIf the object is empty, or no values are
true: the function should return an empty arrayYou 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