简体   繁体   中英

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.

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

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

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)

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM