Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
How to filter an array of objects based on another array of object's property value?

I have two sample arrays below.

let arr1 = [
  { key: "WIHUGDYVWJ", className: "science" },
  { key: "qwljdhkuqwdnqwdk", className: "english" },
  { key: "likubqwd", className: "robotics" }
];
let arr2 = [
  { key: "WIHUGDYVWJ", title: "math" },
  { key: "qwljdhkuqwdnqwdk", title: "english" },
  { key: "likubqwd", title: "robotics" }
];
  1. How can I filter arr1 to get only items that have 'className' value that matches arr2's item's 'title' value? (expecting only items with 'english' and 'robotics' to remain)

  2. How can I filter arr1 to get only items that have 'className' value that do not match arr2's item's 'title' value? (expecting only items with 'science' to remain)

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let arr1 = [
  { key: "WIHUGDYVWJ", className: "science" },
  { key: "qwljdhkuqwdnqwdk", className: "english" },
  { key: "likubqwd", className: "robotics" },
  { key: "abcd", className: "history" }
];

let arr2 = [
  { key: "WIHUGDYVWJ", title: "math" },
  { key: "qwljdhkuqwdnqwdk", title: "english" },
  { key: "likubqwd", title: "robotics" }
];

// q1 answer
console.log(arr1.map(arr1Item => arr2.filter(arr2Item => arr1Item.className === arr2Item.title)).flat());

// q2 answer
console.log(arr1.filter(arr1Item => !arr2.some(arr2Item => arr2Item.title === arr1Item.className)));

i wrote without using a for, if/else statement as much as possible. this code may not be the best. i think it could be more improved.

I hope my answer is helpful

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!