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

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

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

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