0

I have some angular code that I am working on. The goal is to use as little angular 1.x functionality as possible since this will be getting refactored soon.

I am comparing an array

  let subscription = [
  "Cinemax Subscription", 
  "Disney Subscription", 
  "Encore Subscription", 
  "Epix Subscription", 
  "HBO Subscription", 
  "MLB Subscription", 
  "NBA Subscription", 
  "NHL Subscription", 
  "Division"
]

to an array of objects with a key value relationship that houses 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 having a difficult time with the indexes and the looping of this.

let = profilesFiltered = [];

Initially I tried to use a filter and angular.for Each 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);
        }
    })
});

I am seeing inside the if statement that

key.qualifiers[index].name // 'Number of adults in the household'

Starts out correct in comparison to

subscription[0]   // however, it will continue to 1 on the first object which isn't there.  

I see how it is starting to fail. But I am not sure how to get properly loop through the qualifiers in the **profiles ** array.

The desired result is as those house Division which is the last array item in the index.

profilesFiltered = [
    {
    name: '75k',
    qualifers: [
      {
        name: 'Division'
      },
      {
        name: 'Income'
      }
    ]
  },
  {
      name: 'Time Warner',
      qualifers: [
          {
              name: 'Division'
          }
      ]
  }
]

Any feedback is greatly appreciated at this point.

Thanks

1
  • Your problem is, at least for me, still very unclear. Your code has a lot of mistakes in it. For Example, you are not returning a boolean in your filter function. So your filteredProfiles will always be empty. You are not running through the qualifiers array - which is spelled wrong in the objects after the first btw - you are only looking for the first qualifier. ETC. You could provide a small example which is self explaining what you are trying to achieve. Commented Jul 13, 2017 at 8:03

3 Answers 3

2

Just filter the profiles, using a combination of filter() and some():

profiles.filter(v => v.qualifiers.some(q => subscription.includes(q.name)));

let subscription = [
  "Cinemax Subscription",
  "Disney Subscription",
  "Encore Subscription",
  "Epix Subscription",
  "HBO Subscription",
  "MLB Subscription",
  "NBA Subscription",
  "NHL Subscription",
  "Division"
]

let profiles = [{
    name: "1+ Adults in Household",
    qualifiers: [{
      name: 'Number of adults in the household'
    }]
  },
  {
    name: '75k',
    qualifiers: [{
        name: 'Division'
      },
      {
        name: 'Income'
      }
    ]
  },
  {
    name: 'Time Warner',
    qualifiers: [{
      name: 'Division'
    }]
  }
]

let res = profiles.filter(v => v.qualifiers.some(q => subscription.includes(q.name)));


console.log(res)

Sign up to request clarification or add additional context in comments.

Comments

1

Are you looking for something like this, checkout my code

let profiles = [
    {
        name:"1+ Adults in Household",
        qualifiers: [
            {
                name: 'Number of adults in the household'
            }
        ]
    },
    {
      name: '75k',
      qualifiers: [
        {
          name: 'Division'
        },
        {
          name: 'Income'
        }
      ]
  },
  {
      name: 'Time Warner',
      qualifiers: [
          {
              name: 'Division'
          }
      ]
  }
];

let subscription = [
  "Cinemax Subscription", 
  "Disney Subscription", 
  "Encore Subscription", 
  "Epix Subscription", 
  "HBO Subscription", 
  "MLB Subscription", 
  "NBA Subscription", 
  "NHL Subscription", 
  "Division"
];

let profilesFiltered = profiles.filter(function (a) {
  // first filter all element in the profiles array 
  // check if their qualifiers array is not empty
  return a.qualifiers.some(function (b) {
    // filter the qualifiers array by checking the property name 
    // is it muching any element from subscription array
    return subscription.indexOf(b.name)!==-1;
  });
});

console.log(profilesFiltered);

3 Comments

Pretty much so. Thanks!
Using filter at a.qualifiers.filter... is not a very good idea. While some will stop iterating at the first match, filter will always iterate the whole array. That's unnecessary work.
You must use !== -1 at indexOf :-)
-1

Here is a working code for your data structure. It is based on using reduce function. If you need to have no duplicates - answer here.

https://jsbin.com/dexagiresa/2/edit?js,output

let profiles = [
{
    name:"1+ Adults in Household",
    qualifiers: [
        {
            name: 'Number of adults in the household'
        }
    ]
},
{
    name: '75k',
    qualifiers: [
        {
            name: 'Division'
        },
        {
            name: 'Income'
        }
    ]
},
{
    name: 'Time Warner',
    qualifiers: [
        {
            name: 'Division'
        }
    ]
}
];



let subscription = [
  "Cinemax Subscription", 
  "Disney Subscription", 
  "Encore Subscription", 
  "Epix Subscription", 
  "HBO Subscription", 
  "MLB Subscription", 
  "NBA Subscription", 
  "NHL Subscription", 
  "Division"
];


var filteredProfiles = profiles.reduce(function (collectedResults, profile) {

    var newResults = profile.qualifiers.reduce(function(foundQualifiers, qualifier) {

        if(subscription.indexOf(qualifier.name) > -1) {
            return foundQualifiers.concat(qualifier.name);
        } else {
            return foundQualifiers;
        }

    }, []);

    return collectedResults.concat(newResults);

}, []);

document.write(filteredProfiles);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.