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?