คำอธิบาย
ใช้ map() ดึงค่า name ออกมา แล้ว filter() ร่วมกับ includes() เพื่อหาค่าที่ซ้ำกันระหว่างสองอาร์เรย์
ตัวอย่างโค้ด
const firstArray = [
{ "name": "John Doe" },
{ "name": "John Smith" },
{ "name": "David Miller" },
{ "name": "Bob Taylor" },
{ "name": "Carol taylor" },
{ "name": "Adam Smith" }
];
const secondArray = [
{ "name": "Adam Smith" },
{ "name": "John Doe" },
{ "name": "David Miller" },
{ "name": "James Taylor" }
];
const getAllValue = ({ 'name': name }) => name;
const result = firstArray
.map(getAllValue)
.filter(value => secondArray.map(getAllValue).includes(value));
console.log(result);
วิธีรัน
node fileName.js
ผลลัพธ์
[ 'John Doe', 'David Miller', 'Adam Smith' ]