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

110
Views
Simplify filtering between 2 arrays

I have the following working method. It creates an array of items (which I then use to populate a select dropdown) that haven't otherwise been previously used throughout the rest of the form array. I'm just wondering if there is a shorter more elegant way of doing all this?

uniqueChoiceLocaleItems(currentValue) {

        // Create an array of all the ids already taken. ie. ["en", "fr", "es"];
        var idsTaken = [];
        this.myForm['controls'].languages['controls'].forEach((x, index) => {
            if ((x['controls'].id.value != currentValue)) {
                idsTaken.push(x['controls'].id.value);
            }
        });

        // Loop through our main master 'this.locales' array which has ALL the locales in the world and filter out all the ids from the 'idsTaken' array
        var uniqueItems = [];
        this.locales.forEach((x, index) => {
            if (idsTaken.indexOf(x.code) !== -1) {
            } else {
                uniqueItems.push(x);
            }
        });
       
        return uniqueItems;
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use a Set and its constructor callback to create it. Then delete currentValue from that set (it doesn't matter whether it was actually in it or not). Finally use filter:

uniqueChoiceLocaleItems(currentValue) {
    var idsTaken = new Set(
        this.myForm.controls.languages.controls.map(x => x.controls.id.value)
    );
    idsTaken.delete(currentValue);
    return this.locales.filter(x => !idsTaken.has(x.code));
}
about 4 years ago · Juan Pablo Isaza Report

0

Simple and modified version.

var idsTaken = this.myForm["controls"].languages["controls"].filter(
  (x) => x["controls"].id.value != currentValue
).map((x) => x["controls"].id.value);

var uniqueItems = this.locales.filter((x) => idsTaken.indexOf(x.code) === -1);
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!