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

131
Views
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 answers
Answer question

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 Report

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 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!