vbernal
vbernal

Reputation: 713

How to compare two arrays in JavaScript? But the key is a string

I'm beginner in JavaScript, I've two arrays.

I must compare and find out if there are repeated values between them. I am not able to do this because the key of array is a string. As you can see:

[
  { "species-name": "Cana-de-açucar"},
  { "species-name": "Citros"},
  { "species-name": "Eucalipto"},
  { "species-name": "Feijão"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"},
  { "species-name": "Fruticultura"},
  { "species-name": "Girassol"},
  { "species-name": "Hortaliças"},
  { "species-name": "Mandioca"}
]

And the other array:

[
  { "species-name": "Eucalipto"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"}
]

Can someone help me? Thank you in advance.

Upvotes: 1

Views: 110

Answers (4)

kimlianlopez
kimlianlopez

Reputation: 116

Use Array.filter() to iterate through the first array, and only return if you find that value in arr2. Then use Array.map() in the new filtered array, to just return values rather than objects.

const arr1 = [
  { "species-name": "Cana-de-açucar"},
  { "species-name": "Citros"},
  { "species-name": "Eucalipto"},
  { "species-name": "Feijão"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"},
  { "species-name": "Fruticultura"},
  { "species-name": "Girassol"},
  { "species-name": "Hortaliças"},
  { "species-name": "Mandioca"}
];

const arr2 = [
  { "species-name": "Eucalipto"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"}
];

const duplicateValues = arr1.filter(e => {
    return arr2.find(i => i['species-name'] === e['species-name'])
}).map(duplicate => duplicate['species-name']);

console.log(duplicateValues);
// expected output: ["Eucalipto", "Flor de Corte", "Floresta"]

Upvotes: 0

0leg
0leg

Reputation: 14144

Object-structure-agnostic (without a need to deconstruct an object use):

const arr1 = [
  { "species-name": "Cana-de-açucar"},
  { "species-name": "Citros"},
  { "species-name": "Eucalipto"},
  { "species-name": "Feijão"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"},
  { "species-name": "Fruticultura"},
  { "species-name": "Girassol"},
  { "species-name": "Hortaliças"},
  { "species-name": "Mandioca"}
];

const arr2 = [
  { "species-name": "Eucalipto"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"},
  { "species-name": "not-a-duplicate"},
];

const arr2String = JSON.stringify(arr2);
const duplicates = arr1.filter(x => arr2String.includes(JSON.stringify(x)));
console.log(duplicates)

Upvotes: 0

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48610

If you want the intersection of two arrays, then you will have to see which one is longer. The longest one will be filtered, and the shorter one will be used as an array to search within.

const first = [
  { "species-name": "Cana-de-açucar"},
  { "species-name": "Citros"},
  { "species-name": "Eucalipto"},
  { "species-name": "Feijão"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"},
  { "species-name": "Fruticultura"},
  { "species-name": "Girassol"},
  { "species-name": "Hortaliças"},
  { "species-name": "Mandioca"}
]

const second = [
  { "species-name": "Eucalipto"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"}
]

const intersection = (longer, shorter, key) => {
  let tmp;
  if (shorter.length > longer.length) {
    tmp = shorter, shorter = longer, longer = tmp; // Swap
  }
  const vals = shorter.map(entry => entry[key]);
  return longer.filter(entry => vals.find(v => v === entry[key]));
}

const key = 'species-name';
const third = intersection(first, second, key);

console.log(third);
console.log(third.flatMap(Object.values));
.as-console-wrapper { top: 0; max-height: 100% !important; }

Upvotes: 1

Guerric P
Guerric P

Reputation: 31815

This is a simple solution to achieve what you need:

const arr1 = [
  { "species-name": "Cana-de-açucar"},
  { "species-name": "Citros"},
  { "species-name": "Eucalipto"},
  { "species-name": "Feijão"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"},
  { "species-name": "Fruticultura"},
  { "species-name": "Girassol"},
  { "species-name": "Hortaliças"},
  { "species-name": "Mandioca"}
];

const arr2 = [
  { "species-name": "Eucalipto"},
  { "species-name": "Flor de Corte"},
  { "species-name": "Floresta"},
  { "species-name": "Non duplicate value" }
];

const extractValue = ({ 'species-name': speciesName }) => speciesName;

const duplicateValues = arr1
    .map(extractValue)
    .filter(x => arr2
        .map(extractValue)
        .includes(x)
    );

console.log(duplicateValues);

Upvotes: 3

Related Questions