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

134
Views
best way to get unique values of 2 arrays which different structure?

I have 2 arrays like this:

blockedUsers = ['u1', 'u2', 'u3']
videoList = [
    {
        id: 1,
        mp4URL: '...mp4',
        user: {
            id: 'u1',
            name: 'User 1'
        }
    },
    {
        id: 2,
        mp4URL: '...mp4',
        user: {
            id: 'u2',
            name: 'User 1'
        }
    },
    {
        id: 3,
        mp4URL: '...mp4',
        user: {
            id: 'u5',
            name: 'User 1'
        }
    }
]

I want to remove blocked users from video array. At final I will get array has 1 video from u5. How to do that?

Thank you

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

0

Filter out elements where user id is not included in blocked users.

videoList.filter(v => !blockedUsers.includes(v.user.id))
about 4 years ago · Juan Pablo Isaza Report

0

You can filter the list by using the Array.prototype.includes() method.

const
  blockedUsers = ['u1', 'u2', 'u3'],
  videoList = [
    { id: 1, mp4URL: '...mp4', user: { id: 'u1', name: 'User 1' } },
    { id: 2, mp4URL: '...mp4', user: { id: 'u2', name: 'User 1' } },
    { id: 3, mp4URL: '...mp4', user: { id: 'u5', name: 'User 1' } }
  ],
  allowedVideos = videoList.filter(({ user: { id } }) => !blockedUsers.includes(id));

console.log(allowedVideos);
.as-console-wrapper { top: 0; max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Report

0

There are two ways

First way, you could filter the videoList and iterate to check if the user is in the blocked list. We could do this with .includes for blockedUsers, but this way will result in the complexity of O(n*m), given that n is the length of blockedUsers and m is the length of videoList

Second way, you could first turn the blockedUsers into a hash table using Set. This will reduce the query time complexity for blockedUsers from O(n) to O(1). In this way, the overall time complexity would be O(n + m), which is better than the first way

const blockedUsers = ["u1", "u2", "u3"]
const videoList = [ { id: 1, mp4URL: "...mp4", user: { id: "u1", name: "User 1", }, }, { id: 2, mp4URL: "...mp4", user: { id: "u2", name: "User 1", }, }, { id: 3, mp4URL: "...mp4", user: { id: "u5", name: "User 1", }, }, ]

const blockedUsersHashTable = new Set(blockedUsers)

const res = videoList.filter(
  ({ user: { id } }) => !blockedUsersHashTable.has(id)
)

console.log(res)

If time complexity is not your concern, just go with the first way.

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!