如何在JavaScript中比较两个数组?但关键是一个字符串

如何在JavaScript中比较两个数组?但关键是一个字符串,javascript,Javascript,我是JavaScript的初学者,我有两个数组 我必须比较并找出它们之间是否存在重复值。我不能这样做,因为数组的键是字符串。如你所见: [ { "species-name": "Cana-de-açucar"}, { "species-name": "Citros"}, { "species-name": "Eucalipto"}, { "specie

我是JavaScript的初学者,我有两个数组

我必须比较并找出它们之间是否存在重复值。我不能这样做,因为数组的键是字符串。如你所见:

[
  { "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"}
]
另一个数组:

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

有人能帮我吗?提前感谢。

这是一个简单的解决方案,可以满足您的需求:

const arr1=[
{“物种名称”:“Cana-de-açucar”},
{“物种名称”:“Citros”},
{“物种名称”:“Eucalipto”},
{“物种名称”:“Feijão”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“Fruticultura”},
{“物种名称”:“吉拉索尔”},
{“物种名称”:“Hortaliças”},
{“物种名称”:“Mandioca”}
];
常数arr2=[
{“物种名称”:“Eucalipto”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“非重复值”}
];
const extractValue=({‘物种名称’:speciesName})=>speciesName;
常量重复值=arr1
.map(提取值)
.filter(x=>arr2
.map(提取值)
.包括(x)
);

console.log(重复值)这是一个简单的解决方案,可以满足您的需求:

const arr1=[
{“物种名称”:“Cana-de-açucar”},
{“物种名称”:“Citros”},
{“物种名称”:“Eucalipto”},
{“物种名称”:“Feijão”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“Fruticultura”},
{“物种名称”:“吉拉索尔”},
{“物种名称”:“Hortaliças”},
{“物种名称”:“Mandioca”}
];
常数arr2=[
{“物种名称”:“Eucalipto”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“非重复值”}
];
const extractValue=({‘物种名称’:speciesName})=>speciesName;
常量重复值=arr1
.map(提取值)
.filter(x=>arr2
.map(提取值)
.包括(x)
);

console.log(重复值)如果需要两个数组的交集,则必须查看哪一个更长。最长的一个将被过滤,较短的一个将被用作数组以在其中搜索

const first=[
{“物种名称”:“Cana-de-açucar”},
{“物种名称”:“Citros”},
{“物种名称”:“Eucalipto”},
{“物种名称”:“Feijão”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“Fruticultura”},
{“物种名称”:“吉拉索尔”},
{“物种名称”:“Hortaliças”},
{“物种名称”:“Mandioca”}
]
常数秒=[
{“物种名称”:“Eucalipto”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”}
]
常量交点=(长、短、关键点)=>{
让tmp;
如果(较短的长度>较长的长度){
tmp=shorter,shorter=longer,longer=tmp;//交换
}
const vals=shorter.map(entry=>entry[key]);
返回更长的.filter(entry=>vals.find(v=>v==entry[key]);
}
常量键='物种名称';
常数三=交点(第一、第二、关键点);
控制台日志(第三);
log(third.flatMap(Object.values))

.as-console-wrapper{top:0;max-height:100%!important;}
如果需要两个数组的交集,则必须查看哪个数组更长。最长的一个将被过滤,较短的一个将被用作数组以在其中搜索

const first=[
{“物种名称”:“Cana-de-açucar”},
{“物种名称”:“Citros”},
{“物种名称”:“Eucalipto”},
{“物种名称”:“Feijão”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“Fruticultura”},
{“物种名称”:“吉拉索尔”},
{“物种名称”:“Hortaliças”},
{“物种名称”:“Mandioca”}
]
常数秒=[
{“物种名称”:“Eucalipto”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”}
]
常量交点=(长、短、关键点)=>{
让tmp;
如果(较短的长度>较长的长度){
tmp=shorter,shorter=longer,longer=tmp;//交换
}
const vals=shorter.map(entry=>entry[key]);
返回更长的.filter(entry=>vals.find(v=>v==entry[key]);
}
常量键='物种名称';
常数三=交点(第一、第二、关键点);
控制台日志(第三);
log(third.flatMap(Object.values))
.as控制台包装{top:0;最大高度:100%!important;}
对象结构不可知(无需解构对象使用):

const arr1=[
{“物种名称”:“Cana-de-açucar”},
{“物种名称”:“Citros”},
{“物种名称”:“Eucalipto”},
{“物种名称”:“Feijão”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“Fruticultura”},
{“物种名称”:“吉拉索尔”},
{“物种名称”:“Hortaliças”},
{“物种名称”:“Mandioca”}
];
常数arr2=[
{“物种名称”:“Eucalipto”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“不重复”},
];
const arr2String=JSON.stringify(arr2);
constduplicates=arr1.filter(x=>arr2String.includes(JSON.stringify(x));
console.log(重复)
对象结构不可知(无需解构对象使用):

const arr1=[
{“物种名称”:“Cana-de-açucar”},
{“物种名称”:“Citros”},
{“物种名称”:“Eucalipto”},
{“物种名称”:“Feijão”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“Fruticultura”},
{“物种名称”:“吉拉索尔”},
{“物种名称”:“Hortaliças”},
{“物种名称”:“Mandioca”}
];
常数arr2=[
{“物种名称”:“Eucalipto”},
{“物种名称”:“Flor de Corte”},
{“物种名称”:“Floresta”},
{“物种名称”:“不重复”},
];
const arr2String=JSON.stringify(arr2);
constduplicates=arr1.filter(x=>arr2String.includes(JSON.stringify(x));
log(重复)
用于迭代第一个数组,仅当
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"]