Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda