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

143
Visualizações
How to output an array containing only the array to search?

The current code returns all found elements if the array contains one element comp = ['bbb'] see example. How can I change the code to output the array as a strict match, i.e. log [[ 'a', 'bbb', 'c' ]] ? For other cases, no changes are required. I've attached the actual code and wrote the output examples.

function datasearch() {
 
            let array = [['a', 'bbb', 'c'], ['r', 'bbbfff', 'y',],['w', 'bbbfffkkk', 'u',]]
            let comp = ['bbb'];
            let ans = [];
            for (let i = 0; i < array.length; i++) {        
                for (el of array[i]) {
                    if (comp.every(y => el.includes(y))) {
                        ans.push(array[i]);
                        break;
                    }
                }
            }
            console.log(ans);
            
    }


now
comp = ['bbb']
log [ [ 'a', 'bbb', 'c' ],[ 'r', 'bbbfff', 'y' ],[ 'w', 'bbbfffkkk', 'u' ] ]

Expected result for search with one array element

    comp = ['bbb']
    log [[ 'a', 'bbb', 'c' ]]

For other cases, everything is as in the current code For example:

comp = ['bbb','fff']
log  [ [ 'r', 'bbbfff', 'y' ], [ 'w', 'bbbfffkkk', 'u' ] ]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It seems like you need two separate behaviors, which need to be treated independently.

  1. When your search array comp has only one element, you need to check for an exact match in the source array array.
  2. When your search array comp contains multiple elements, you only check if the elements are present in the source array (current implementation).

Code will become:

function datasearch() {

    let array = [['a', 'bbb', 'c'], ['r', 'bbbfff', 'y',], ['w', 'bbbfffkkk', 'u',]]
    let comp = ['bbb'];
    let ans = [];
    for (let i = 0; i < array.length; i++) {
        if (comp.length == 1) {
          for (el of array[i]) {
            if (el === comp[0]) {
              ans.push(array[i]);
              break;
            }
          }
        } else {
            for (el of array[i]) {
                if (comp.every(y => el.includes(y))) {
                    ans.push(array[i]);
                    break;
                }
            }
        }
    }
    console.log(ans);

}
about 4 years ago · Juan Pablo Isaza Relatório

0

If I understood you correctly, then perhaps such a solution can help you:

function datasearch(comp = [], strictCompare = false) {
  const array = [
    ['a', 'bbb', 'c'],
    ['r', 'bbbfff', 'y', ],
    ['w', 'bbbfffkkk', 'u', ]
  ]
  const ans = [];
  for (let i = 0; i < array.length; i++) {
    for (el of array[i]) {
      if (comp.every(y => (strictCompare) ? el === y : el.includes(y))) {
        ans.push(array[i]);
        break;
      }
    }
  }
  return ans;
}

console.log('not strict', datasearch(['bbb']));
console.log('strict', datasearch(['bbb'], true));
console.log('combine(strict & not strict)', datasearch(['bbb'], true).concat(datasearch(['kkk'])));

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