DEV Community

taiseen
taiseen

Posted on • Edited on

filter [array] base on other [array]

assume we have an [array of {objects}] & we want to filter these {objects} base on any other [array values]...

to fulfill this requirement, we can implement our code by this approach:-

const ids = [1, 2, 3, 4];

const userObj = [
  {
    id: 1,
    name: 'Jon',
  },
  {
    id: 20,
    name: 'Sam',
  },
  {
    id: 30,
    name: 'Tom',
  },
  {
    id: 4,
    name: 'Zen',
  },
];

const result = userObj.filter(user => ids.includes(user.id));

console.log(result);

// console output...

(2) [{...}, {...}]
0: {id: 1, name: "Jon"}
1: {id: 4, name: "Zen"}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay