'Return simple array from array of objects

Let's say we have this array of objects

array = [
    { id: 1, type: 'car' },
    { id: 2, type: 'bus' },
    { id: 3, type: 'train' }
];

and I want to loop through this array to end up with:

result = ['car','bus','train']

so easy, I did it below

result: string[] = [];
array.forEach((item) => {
    result.push(item.type);
});

...but it sounds like too much primitive code, is there a better solution in TypeScript?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source