So, I get this rather complex array from the API with a lot of nested arrays with objects, etc. It looks like this:
public data: any[] = [
{
language: 'Dutch',
sources: [
{
source: 'De Redactie',
channels: [
{ channel: 'binnenland', value: false },
{ channel: 'buitenland', value: false },
{ channel: 'sport', value: false },
{ channel: 'cultuur en media', value: false },
{ channel: 'politiek', value: false },
{ channel: 'financien', value: false }
]
},
{
source: 'Tweakers',
channels: [
{ channel: 'hardware', value: false },
{ channel: 'software', value: false },
{ channel: 'tech', value: false },
{ channel: 'smartphones', value: false },
{ channel: 'audio', value: false },
{ channel: 'video', value: false }
]
}
]
},
{
language: 'English',
sources: [
{
source: 'BBC',
channels: [
{ channel: 'news', value: false },
{ channel: 'sport', value: false },
{ channel: 'weather', value: false },
{ channel: 'travel', value: false },
{ channel: 'politics', value: false }
]
},
{
source: 'Fox News',
channels: [
{ channel: 'u.s.', value: false },
{ channel: 'world', value: false },
{ channel: 'opinion', value: false },
{ channel: 'politics', value: false },
{ channel: 'entertainment', value: false },
{ channel: 'business', value: false }
]
}
]
}
]
And I can extract the value I want (channel property) with this function:
setChannel(channel: string, source: string, language: string) {
for (const i of this.data) {
if (i.language === language) {
for (const j of i.sources) {
if (j.source === source) {
for (const k of j.channels) {
if (k.channel === channel) {
console.log(k.channel);
}
}
}
}
}
}
}
Now I am sure that lodash has a function to simplify this. Because triple nested and if that is not a good way of coding. But I can not find it in the documentation. Can someone point me in the right direction?