Oleksandr Tril
Oleksandr Tril

Reputation: 27

How can I filter specific object value by the parameter of another object value

This is the input. In the "posts" property, I have all of the posts. I need to filter the posts by "postIds" property.

   const users = [
        { id: 1, name: 'Dan', postIds: [11, 22],posts:[
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },
    ]} ,
        { id: 2, name: 'Mike', postIds: [33], posts:[
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },
    ]},
        { id: 3, name: 'Lola', postIds: [44], posts: [
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },
    ]}

This is how the output should look alike:

   const users = [
        { id: 1, name: 'Dan', postIds: [11, 22],posts:[
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
    ] ,
        { id: 2, name: 'Mike', postIds: [33], posts:[
        { id: 33, title: 'How to can I do it' },
    ]},
        { id: 3, name: 'Lola', postIds: [44], posts: [
        { id: 44, title: 'How to he can do it' },
    ]}

I do believe that I have to work with "filter" and "some", but the option that I tried is not relevantly working:

const filterUserPosts = users.filter(obj=>obj.posts.id===obj.some(postIds))

What is the best option?

Upvotes: 0

Views: 30

Answers (3)

Barmar
Barmar

Reputation: 781210

The array you want to filter is obj.posts, not updUsers (which I assume is the same as users).

You need to loop over users to filter each of their posts properties.

You can use includes() to tell if the id of the post is in postIds.

const users = [
        { id: 1, name: 'Dan', postIds: [11, 22],posts:[
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },
    ]} ,
        { id: 2, name: 'Mike', postIds: [33], posts:[
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },
    ]},
        { id: 3, name: 'Lola', postIds: [44], posts: [
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },
    ]}];


users.forEach(u => u.posts = u.posts.filter(p => u.postIds.includes(p.id)));

console.log(users);

Upvotes: 2

Dom
Dom

Reputation: 734

Try this:

const usersWithFilteredPosts = users.map((user) => ({
  ...user,
  posts: user.posts.filter(({ id }) => user.postIds.includes(id)),
}));

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370819

You need to save the postIds array somewhere first, then call .filter on the posts array, filtering by whether the element being iterated over has an ID in the postIds:

const users = [
        { id: 1, name: 'Dan', postIds: [11, 22],posts:[
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },
    ]} ,
        { id: 2, name: 'Mike', postIds: [33], posts:[
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },
    ]},
        { id: 3, name: 'Lola', postIds: [44], posts: [
        { id: 11, title: 'How to make it fast' },
        { id: 22, title: 'How to make it clearly' },
        { id: 33, title: 'How to can I do it' },
        { id: 44, title: 'How to he can do it' },]}
    ]
const output = users.map(
  user => ({
    ...user,
    posts: user.posts.filter(
      post => user.postIds.includes(post.id)
    )
  })
);
console.log(output);

Upvotes: 1

Related Questions