赞
踩
在array1中,匹配id,name与array2一致的对象,并返回。
- const array1 = [
- { id: 1, name: 'John', title: 'aaa' },
- { id: 2, name: 'Jane', title: 'aaa' },
- { id: 3, name: 'Mike', title: 'aaa' },
- { id: 4, name: 'Alice', title: 'aaa' },
- { id: 5, name: 'Alice', title: 'aaa' },
- ];
-
- const array2 = [
- { id: 2, name: 'Jane Doe' },
- { id: 4, name: 'Alice' },
- ];
-
- const filteredArray = array1.filter(obj1 => {
- return array2.some(obj2 => obj2.id === obj1.id && obj2.name === obj1.name);
- });
-
- console.log(filteredArray); //[0:{ id: 4, name: 'Alice', title: 'aaa' },]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。