2

HI All I am having two array of object my aim is to compare them and filter out the matched result my data looks like this

let data1 = [
    {
        name:'tom',
        process:'flipkart',
        master:'pharma',
        profiles: [
            {
                level:'begginer',
                language:'hindi',
                role:['flp_admin','flp_teacher']
            }
        ]
    
    
    },
    {
        name:'jeo',
        process:'amazon',
        master:'science',
        profiles: [
            {
                level:'begginer',
                language:'english',
                role:['amz_admin']
            }
        ]
    
    
    },
    {
        name:'jerry',
        process:'email',
        master:'it',
        profiles: [
            {
                level:'begginer',
                language:'urdu',
                role:['eml_teacher']
            }
        ]
    
    }
]
let data2 = [
    {
        masterName:'Pharma',
        businessProcess: [
            { label:'flipkart', value:'flipkart' },
            { label:'amazon', value:'amazon' }
        ]
    },
        {
        masterName:'science',
        businessProcess: [
            { label:'flipkart', value:'flipkart' },
            { label:'amazon', value:'amazon' }
        ]
    },
        {
        masterName:'it',
        businessProcess: [
            { label:'email', value:'email' },
            { label:'amazon', value:'amazon' }
        ]
    }

I want to compare data1 with data2 and return the match from data2 if master of data1 matches with masterName of data2 and if business of data1 matches with businessProcess.label of data2.

Could anyone please tell me how can I do it?

2

1 Answer 1

0

You can use Array.filter and Array.find to loop over and find the matching items:

let data1 = [{
    name: 'tom',
    process: 'flipkart',
    master: 'pharma',
    profiles: [{
      level: 'begginer',
      language: 'hindi',
      role: ['flp_admin', 'flp_teacher']
    }]


  },
  {
    name: 'jeo',
    process: 'amazon',
    master: 'science',
    profiles: [{
      level: 'begginer',
      language: 'english',
      role: ['amz_admin']
    }]


  },
  {
    name: 'jerry',
    process: 'email',
    master: 'it',
    profiles: [{
      level: 'begginer',
      language: 'urdu',
      role: ['eml_teacher']
    }]

  }
]
let data2 = [{
    masterName: 'Pharma',
    businessProcess: [{
        label: 'flipkart',
        value: 'flipkart'
      },
      {
        label: 'amazon',
        value: 'amazon'
      }
    ]
  },
  {
    masterName: 'science',
    businessProcess: [{
        label: 'flipkart',
        value: 'flipkart'
      },
      {
        label: 'amazon',
        value: 'amazon'
      }
    ]
  },
  {
    masterName: 'it',
    businessProcess: [{
        label: 'email',
        value: 'email'
      },
      {
        label: 'amazon',
        value: 'amazon'
      }
    ]
  }
];
console.log(data1.filter((d) => {
  return data2.find((d2) => {
    //check if data matername equals data1 master
    // or if data1.process value exists in one of the item of businessProcess as value 
    return d2.masterName == d.master || d2.businessProcess.find(b => b.value === d.process);
  });

}));

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

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.