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

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

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 Report

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