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

121
Visualizações
How to check in array if it contains given element

I am trying below code but its not working so first am trying to get a person department Name currently, hard coded it to check

var arr=[]
let CurrDept = "AB-CDE-F";
var Detept=CurrDept.substring(0,5);
arr.push(Detept)

Now in below line of code i am trying this line so it should exclude all results which start from AB-CD

var Userprofiledept=data.value[0].UserId.Department;
const isInArray = arr.indexOf(Userprofiledept) > -1;

isInArray should be false which can be put in my condition but it always give true

Can anyone help ?

So current user department may be AB-CDE-F and data i am getting from my rest call may have lots of department for many users AB-CDE-F,AB-CDE-F,AB-CDE,AB-CD,AB-C

so only want to exclude results which are AB-CDE-F,AB-CDE-F,AB-CDE,AB-CD as they are starting with AB-CD

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could move the functionality into a function and check if the substring exists and add, if necessary.

const
    add = string => {
        string = string.slice(0, 5);
        if (array.includes(string)) return;
        array.push(string);
    }
    array = [];

add('AB-CDE-F');
console.log(array);

add('AB-CDE-F');
console.log(array);

add('AB-CDE');
console.log(array);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.prototype.filter and String.prototype.startsWith.

const 
  data = {value: [{ UserId: { ID: 14, Email: "sdfds", Department: "AB-CD-EF" } }, { UserId: { ID: 14, Email: "sdfds", Department: "AB-CD" } }, { UserId: { ID: 14, Email: "sdfds", Department: "AB-C" } }]},
  discard = "AB-CD",
  res = data.value.filter((v) => !v.UserId.Department.startsWith(discard));

console.log(res);

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