Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda