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

124
Views
Remove an item from object based on existence in second object

I have the following 2 objects. How can I remove items from listProposalProducts that don't have a coverageLineId that matches an id from coverageLinesSelectedItems? In this example, I want to remove item 1 from listProposalProducts because an id of 2 does not exist in coverageLinesSelectedItems. I think I'm looking for the opposite of the pop() method but I'm not sure how to get the correct index to splice out of listProposalProducts.

let listProposalProducts = [
    0: {productId: 101391, coverageLineId: 1}, 
    1: {productId: 101591, coverageLineId: 2},
    2: {productId: 151391, coverageLineId: 3}
]

let coverageLinesSelectedItems =  [
    0: {id: 1, itemName: 'Medical'},
    3: {id: 3, itemName: 'Vision'}
]
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

You could use Array.filter combined with Array.some to filter the results. E.g.

let listProposalProducts = [
    0: {productId: 101391, coverageLineId: 1}, 
    1: {productId: 101591, coverageLineId: 2},
    2: {productId: 151391, coverageLineId: 3}
]

let coverageLinesSelectedItems =  [
    0: {id: 1, itemName: 'Medical'},
    3: {id: 3, itemName: 'Vision'}
]

let requiredResult = listProposalProducts.filter(
  item => coverageLinesSelectedItems.some(
    cov => cov.id === item.coverageLineId
  )
);

This keeps only the products whose coverageLineId matches the id of a coverageLinesSelectedItems element.

about 4 years ago · Santiago Gelvez Report

0

A simple straight forward implementation would be:

listProposalProducts.filter((it) => coverageLinesSelectedItems.find((other) => other.id === it.coverageLineId))

If the objects are larger, you might want to compute a map from the lookup object.

const lookup = {}
coverageLinesSelectedItems.forEach((it) => {lookup[it.id] = true;})
listProposalProducts.filter((it) => lookup[it.coverageLineId])

about 4 years ago · Santiago Gelvez 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!