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

185
Views
I have array with objects, I need to fetch all the objects that matches the value of userId starts with 'US'

Below is the array with objects:

myArray:[    
    {"name":"Ram", "email":"ram@gmail.com", "userId":"HB000006"},    
    {"name":"Shyam", "email":"shyam23@gmail.com", "userId":"US000026"},  
    {"name":"John", "email":"john@gmail.com", "userId":"HB000011"},    
    {"name":"Bob", "email":"bob32@gmail.com", "userId":"US000106"}   
    ]}  

I tried this but I am not getting output:

    item= myArray.filter(element => element.includes("US"));

I am new to Angular.

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

0

As noted by @halfer - You need to filter on the property that you are interested in - in this case - 'userId' - you can do this by simply adding the property into the code you already had tried and it will log out the specified items - or alternatively - you can make a utility function that takes the array, property and target string as arguments and this will allo2w you to search / filter other arrays and by any property and target string .

These two options are shown below and both log out the same results.

const myArray = [    
    {"name":"Ram", "email":"ram@gmail.com", "userId":"HB000006"},    
    {"name":"Shyam", "email":"shyam23@gmail.com", "userId":"US000026"},  
    {"name":"John", "email":"john@gmail.com", "userId":"HB000011"},    
    {"name":"Bob", "email":"bob32@gmail.com", "userId":"US000106"}   
    ]
    
// option 1 - direct filtering
const matchingItems = myArray.filter(element => element.userId.includes("US"));
console.log(matchingItems);

// gives - [ { name: 'Shyam', email: 'shyam23@gmail.com', userId: 'US000026' }, { name: 'Bob', email: 'bob32@gmail.com', userId: 'US000106' } ]


//option 2 - create a function that takes arguments and returns the matches
const matches = (arr, prop, str) => {
 return  arr.filter(element => element[prop].includes(str));
}
console.log(matches(myArray, 'userId', 'US'));

// gives - [ { name: 'Shyam', email: 'shyam23@gmail.com', userId: 'US000026' }, { name: 'Bob', email: 'bob32@gmail.com', userId: 'US000106' } ]

about 4 years ago · Juan Pablo Isaza Report

0

let filteredArray = myArray.filter(function (item){
        return item.userId.substring(0,2).includes('US')
    })
    Console.log(filteredArray)

//Output

[ { name: 'Shyam', email: 'shyam23@gmail.com', userId: 'US000026' }, { name: 'Bob', email: 'bob32@gmail.com', userId: 'US000106' } ]

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!