I am currently working on some Angular code with the objective of minimizing the use of Angular 1.x functionality, as it will be refactored in the near future.
My task involves comparing an array:
let subscription = [
"Cinemax Subscription",
"Disney Subscription",
"Encore Subscription",
"Epix Subscription",
"HBO Subscription",
"MLB Subscription",
"NBA Subscription",
"NHL Subscription",
"Division"
]
with an array of objects that have a key-value relationship containing another array:
let profiles = [
{
name:"1+ Adults in Household",
qualifiers: [
{
name: 'Number of adults in the household'
}
]
},
{
name: '75k',
qualifers: [
{
name: 'Division'
},
{
name: 'Income'
}
]
},
{
name: 'Time Warner',
qualifers: [
{
name: 'Division'
}
]
}
]
I am facing challenges with indexing and looping through these arrays.
let profilesFiltered = [];
Initially, I attempted to use a filter and Angular.forEach to compare the arrays.
let filteredProfiles = subscription.filter( function (src) {
angular.forEach(profiles, function (key, index) {
if(src === key.qualifiers[index].name) {
profilesFiltered.push(key);
}
})
});
When inside the if statement, I noticed that:
key.qualifiers[index].name // 'Number of adults in the household'
initially matches:
subscription[0] // but then moves on to 1 in the first object which is not there.
I can see where the issue is arising but I'm unsure how to properly loop through the qualifiers in the profiles array.
The desired outcome is to filter out those profiles that include Division as the last item in the array.
profilesFiltered = [
{
name: '75k',
qualifers: [
{
name: 'Division'
},
{
name: 'Income'
}
]
},
{
name: 'Time Warner',
qualifers: [
{
name: 'Division'
}
]
}
]
Any insights or feedback would be greatly appreciated at this stage.
Thank you.