Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Removing Duplicates From 2 Arrays

I have 2 arrays that I'm trying to compare and get the unique values from:

arr1 = [
   'Material',
   'Style',
   'Size',
   'add another item',
   'THIS IS A TEST OPTION',
   'Title',
   'Color',
   'dfgdfs',
   'teststststst',
   'THIS IS A NEW OPTION'
]

arr2 = [
   { option: 'Title', product: [ [Object] ] },
   { option: 'Style', product: [ [Object] ] },
   { option: 'Material', product: [ [Object] ] },
   { option: 'Size', product: [ [Object], [Object] ] },
   { option: 'Color', product: [ [Object], [Object] ] },
   { option: 'THIS IS A NEW OPTION', product: [ [Object] ] },
   { option: 'THIS IS A TEST OPTION', product: [ [Object] ] },
   { option: 'add another item', product: [ [Object] ] }
]

Expected output:

['dfgdfs','teststststst']

I have no issue pulling out unique values when the arrays are 1 level deep. However, I'm having issues with arr2 being an array of objects.

I've tried:

arr1 = arr1.filter(val => !arr2.includes(val));

Which obviously doesn't work because arr2 is an array of objects.

I've also tried:

 arr1.filter((val, index) => {
      if (arr2[index]) {
        !arr2[index].option.includes(val);
      }
    });

As well as:

arr1.filter((val, index) => {
      allUniqueOptions.forEach((option) => {
        !option.option.includes(val);
      });
    });

This seems like it should be so simple but I've been racking my head for hours. Any idea what I'm doing wrong?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Your filter functions need to return true if the item is to be included, and false if not. Note that your last two examples don't return anything.

The logic that I used in the filter function is -- Does the current element appear as a "option" property in arr2? if yes return false, if no return true.

arr1 = [
   'Material',
   'Style',
   'Size',
   'add another item',
   'THIS IS A TEST OPTION',
   'Title',
   'Color',
   'dfgdfs',
   'teststststst',
   'THIS IS A NEW OPTION'
]

arr2 = [
   { option: 'Title', product: {}},
   { option: 'Style', product: {} },
   { option: 'Material', product: {} },
   { option: 'Size', product: {} },
   { option: 'Color', product: {} },
   { option: 'THIS IS A NEW OPTION', product: {} },
   { option: 'THIS IS A TEST OPTION', product: {} },
   { option: 'add another item', product: {} }
]

const uniques = arr1.filter(item => !arr2.find(el => el.option === item));
console.log(uniques);

about 4 years ago · Juan Pablo Isaza Report

0

You need filter the arr1 and check with some method on each item in the array if there are no equalivient to it exist in the arr2 options.

const arr1 = [
    'Material',
    'Style',
    'Size',
    'add another item',
    'THIS IS A TEST OPTION',
    'Title',
    'Color',
    'dfgdfs',
    'teststststst',
    'THIS IS A NEW OPTION'
 ]
 
 const arr2 = [
    { option: 'Title', product: [ [ {} ] ] },
    { option: 'Style', product: [ [ {} ] ] },
    { option: 'Material', product: [ [ {} ] ] },
    { option: 'Size', product: [ [ {} ], [ {} ] ] },
    { option: 'Color', product: [ [ {} ], [ {} ] ] },
    { option: 'THIS IS A NEW OPTION', product: [ [ {} ] ] },
    { option: 'THIS IS A TEST OPTION', product: [ [ {} ] ] },
    { option: 'add another item', product: [ [ {} ] ] }
 ]

const result =  arr1.filter(i => !arr2.some(o => i === o.option))

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

To add to the above answers.

Another reason your filters didn't work is because

arr2[index] will go out of range.

arr1 had more indexes than arr2

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!