Created
January 18, 2023 22:03
-
-
Save gtchakama/e208438002dc2358a09e5fb9321f8286 to your computer and use it in GitHub Desktop.
filter array of objects by another array of objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const array = [ | |
{ id: 1, name: 'a1', sub: { id: 6, name: 'a1 sub' } }, | |
{ id: 2, name: 'a2', sub: null }, | |
{ id: 3, name: 'a3', sub: { id: 8, name: 'a3 sub' } }, | |
{ id: 4, name: 'a4', sub: null }, | |
{ id: 5, name: 'a5', sub: { id: 10, name: 'a5 sub' } }, | |
]; | |
const anotherArray = [ | |
{ id: 1, name: 'a1', sub: { id: 6, name: 'a1 sub' } }, | |
{ id: 2, name: 'a2', sub: null }, | |
{ id: 5, name: 'a5', sub: { id: 10, name: 'a5 sub' } }, | |
]; | |
const r = array.filter((elem) => !anotherArray.find(({ id }) => elem.id === id) && elem.sub); | |
console.log(r); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment