I use the Angular InMemoryService to store some fake data. I would like to transform the "HashtagDB" property into the array "hashtags". This "hashtags" array should only contain the values, but not the labels, so that I can use it to display it as Angular-Material-Chips.
As far as I understand, HashtagDB is an array property of an unnamed object. Is that correct? How do I transform the data from the array?
export class InMemoryDataService implements InMemoryDbService {
createDb() {
const person = [
{ Id: 1,
HashtagsDB: [ {hashtag: 'world'}, {hashtag: 'digital'}, {hashtag: 'economy'},
];
return {person};
}
}
hashtags: string[] = [/*'world', 'digital', 'economy*/];
You can project the HashtagsDB to a string[] by using map operator.
i.e.
var people = [
{
ID: 1,
HashtagsDB: [{hashtag: 'world'}, {hashtag: 'digital'}, {hashtag: 'economy'}]
}
];
// Hashtags of the first person.
var hashtags = people[0]['HashtagsDB'].map(item => item['hashtag']);
console.log(hashtags);
// Should returns
/*[
"world",
"digital",
"economy"
]*/