By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to filter an array of objects using another array of objects?
JavaScript

Javascript – How to filter an array of objects using another array of objects?

Admin
Last updated: 2024/02/07 at 6:03 PM
Admin
Share
3 Min Read
How to filter an array of objects using another array of objects?

Problem:

I want to filter one array of objects using another array of objects, like in the following code snippet:

Contents
Problem:Solution:
const entries = [
  {
    id: 1,
    name: "Entry 1",
    topics: [
      {
        id: 101,
        name: "topic 1.1",
      },
      {
        id: 102,
        name: "topic 1.2",
      },
      {
        id: 103,
        name: "topic 1.3",
      },
    ],
  },
  {
    id: 2,
    name: "Entry 2",
    topics: [
      {
        id: 201,
        name: "topic 2.1",
      },
      {
        id: 202,
        name: "topic 2.2",
      },
      {
        id: 203,
        name: "topic 2.3",
      },
    ],
  },
  {
    id: 3,
    name: "Entry 3",
    topics: [
      {
        id: 25,
        name: "topic 3.1",
      },
      {
        id: 26,
        name: "topic 3.2",
      },
    ],
  },
];

const filters = [
  {
    id: 1,
    topics: [
      {
        id: 101,
      },
      {
        id: 102,
      },
    ],
  },
  {
    id: 2,
    topics: [],
  },
];

const result = entries
  .filter(({ id, topics }) =>
    filters.some(
      ({ id: idFromFilteres, topics: topicsFromFilters }) =>
        idFromFilteres === id && topicsFromFilters.length > 0,
    ),
  )
  .map(({ id, topics, ...rest }) => ({
    id,
    topics: topics?.filter(({ id: topicId }) =>
      filters.some(({ topics: topicsFromFilter }) =>
        topicsFromFilter.some(
          ({ id: topicIdFromFilter }) => topicIdFromFilter === topicId,
        ),
      ),
    ),
    ...rest,
  }));

console.log(result);

The code is working fine, but I don’t like how I’m iterating again the entries array to filter the topics; notice also that entries with empty topics get filtered as well.

Is there a way to filter this array using a single iteration?

Solution:

Use Array::reduce() to filter and map simultaneously:

const result = entries.reduce((r, entry) => {
  const found = filters.find(({ id }) => entry.id === id);
  if (found) {
    const topics = entry.topics.filter(({ id }) => found.topics.some((topic) => topic.id === id));
    topics.length && r.push({...entry, topics});
  }
  return r;
}, []);

console.log(result);
<script>
const entries = [
  {
    id: 1,
    name: "Entry 1",
    topics: [
      {
        id: 101,
        name: "topic 1.1",
      },
      {
        id: 102,
        name: "topic 1.2",
      },
      {
        id: 103,
        name: "topic 1.3",
      },
    ],
  },
  {
    id: 2,
    name: "Entry 2",
    topics: [
      {
        id: 201,
        name: "topic 2.1",
      },
      {
        id: 202,
        name: "topic 2.2",
      },
      {
        id: 203,
        name: "topic 2.3",
      },
    ],
  },
  {
    id: 3,
    name: "Entry 3",
    topics: [
      {
        id: 25,
        name: "topic 3.1",
      },
      {
        id: 26,
        name: "topic 3.2",
      },
    ],
  },
];

const filters = [
  {
    id: 1,
    topics: [
      {
        id: 101,
      },
      {
        id: 102,
      },
    ],
  },
  {
    id: 2,
    topics: [],
  },
];
</script>

Expand snippet

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Can I use import to read a JSON file in a Next.js 13 API? Javascript – Can I use import to read a JSON file in a Next.js 13 API?
Next Article Show/hide multiple images individually Javascript – Show/hide multiple images individually
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?