• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

116
Vistas
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?

almost 3 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

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

almost 3 years ago · Juan Pablo Isaza Denunciar

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)

almost 3 years ago · Juan Pablo Isaza Denunciar

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

almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda