简体   繁体   中英

javascript comparing an array to an array of objects

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

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) 

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); 

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM