Javascript 查找第二个数组中具有指定属性的所有对象作为第一个数组中的对象

Javascript 查找第二个数组中具有指定属性的所有对象作为第一个数组中的对象,javascript,arrays,object,Javascript,Arrays,Object,假设我有两个对象数组 const arr1 = [ { id: '5f6d8fe8f13352002abe77c5', subject: 'ray@example.com', location: 'Location 2', timestamp: '2020-08-30T18:00:23.000Z', }, { id: '5f6dacdb49b4bf002a94c73f', subject: 'ray@example.com',

假设我有两个对象数组

const arr1 = [
  {
    id: '5f6d8fe8f13352002abe77c5',
    subject: 'ray@example.com',
    location: 'Location 2',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
  {
    id: '5f6dacdb49b4bf002a94c73f',
    subject: 'ray@example.com',
    location: 'Location 3',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
];

const arr2 = [
  {
    id: '5f6d8fc3f13352002abe77c1',
    subject: 'john.doe@example.com',
    location: '101 Dirk Place',
    timestamp: '2020-08-31T18:00:23.000Z',
  },
  {
    id: '5f6d8fd9f13352002abe77c3',
    subject: 'jane.doe@example.com',
    location: 'Location 1',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
  {
    id: '5f6d8fe8f13352002abe77c5',
    subject: 'ray@example.com',
    location: 'Location 2',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
  {
    id: '5f6d99c4f45312002af7e343',
    subject: 'john@example.com',
    location: 'Location 2',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
  {
    id: '5f6d99e41edf47002acd3292',
    subject: 'johno@example.com',
    location: 'Location 3',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
  {
    id: '5f6dacdb49b4bf002a94c73f',
    subject: 'ray@example.com',
    location: 'Location 3',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
  {
    id: '5f6d9e9794a701002a6f9924',
    subject: 'Marcia14@hotmail.com',
    location: 'Bedfordshire kwhlm5 Awesome Concrete Shoes Co',
    timestamp: '2020-08-01T23:05:00.000Z',
  },
]
如何获取arr2中与arr1中的某个项目具有相同位置(即具有“位置2”或“位置3”)但具有不同主题(即不返回arr2中主题属性为“的项目”)的所有项目ray@example.com'). 最好通过使用Javascript数组函数(map、forEach、filter等)

返回的数组应该是

  const arr3 = [
  {
    id: '5f6d99c4f45312002af7e343',
    subject: 'john@example.com',
    location: 'Location 2',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
  {
    id: '5f6d99e41edf47002acd3292',
    subject: 'johno@example.com',
    location: 'Location 3',
    timestamp: '2020-08-30T18:00:23.000Z',
  },
]

谢谢

我将使用该方法从第一个数组中提取您感兴趣的属性(即位置和主题),然后使用该方法过滤第二个数组,并使用该方法提取属性(包括或不包括(!)

const arr1=[{“id”:“5f6d8fe8f13352002abe77c5”,“主题”:ray@example.com,“位置”:“位置2”,“时间戳”:“2020-08-30T18:00:23.000Z”},{“id”:“5F6DACDB49B4B0002A94C73F”,“主题”:ray@example.com,“位置”:“位置3”,“时间戳”:“2020-08-30T18:00:23.000Z”}];
常量arr2=[{“id”:“5f6d8fc3f13352002abe77c1”,“主题”:“john”。doe@example.com“,”位置“:”101德克广场“,”时间戳“:”2020-08-31T18:00:23.000Z“,”{”id“:”5f6d8fd9f13352002abe77c3“,”主题“:”简。doe@example.com,“位置”:“位置1”,“时间戳”:“2020-08-30T18:00:23.000Z”},{“id”:“5f6d8fe8f13352002abe77c5”,“主题”:ray@example.com,“位置”:“位置2”,“时间戳”:“2020-08-30T18:00:23.000Z”},{“id”:“5F6D99C4F4531202AF7E343”,“主题”:john@example.com,“位置”:“位置2”,“时间戳”:“2020-08-30T18:00:23.000Z”},{“id”:“5f6d99e41edf47002acd3292”,“主题”:johno@example.com,“位置”:“位置3”,“时间戳”:“2020-08-30T18:00:23.000Z”},{“id”:“5F6DACDB49B4B0002A94C73F”,“主题”:"ray@example.com,“位置”:“位置3”,“时间戳”:“2020-08-30T18:00:23.000Z”},{“id”:“5F6D9E9794A71002A6F9924”,“主题”:Marcia14@hotmail.com“,”位置“:”贝德福德郡kwhlm5 Awesome混凝土鞋公司“,”时间戳“:”2020-08-01T23:05:00.000Z“}];
const locations=arr1.map(item=>item.location);
const subjects=arr1.map(item=>item.subject);
常量过滤器darray=arr2.filter(
item=>locations.includes(item.location)和&!subject.includes(item.subject)
);
控制台日志(filteredArray)

。作为控制台包装{最大高度:100%!重要;顶部:0;}
谢谢!干净的溶液!