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

349
Views
How to apply search to an array of arrays of strings?

In my case, the search should go through the first [1] elements of nested arrays. I added a loop through the array elements from the answer, but I can't add the found arrays to the array.

function massivs() {
        let array = [['a', 'mango', 'b','c'], ['d', 'applemango', 'e','f'],['k', 'mangoapple', 's','r']];
        let comp = ['apple', 'mango'];
        let ans = [];
        
        for (let i = 0; i < array.length; i++) {        
            for (el of array[i]) {
                if (comp.every(y => el.includes(y))) {
                    ans.push(el);
                }
            }
        }
        console.log(ans);
}

massivs();

[ 'applemango', 'mangoapple' ]


Expected Result [['d', 'applemango', 'e','f'],['k', 'mangoapple', 's','r']]
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You could do it with just one line of code:

const array = [['a', 'mango', 'b','c'], ['d', 'applemango', 'e','f'],['k', 'mangoapple', 's','r']];
const comp = ['apple', 'mango'];

const result = array.filter(x => comp.every(y => x.some(z => z.includes(y))));

console.log(result);

about 4 years ago · Santiago Gelvez Report

0

Replace:

ans.push(el)

With:

ans.push(array[i])
about 4 years ago · Santiago Gelvez Report

0

for of iterates the items of your array, so it's only pushing the element of the array that is included in your search values, push the whole array if found in search and break the loop

 function massivs() {
            let array = [['a', 'mango', 'b','c'], ['d', 'applemango', 'e','f'],['k', 'mangoapple', 's','r']];
            let comp = ['apple', 'mango'];
            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);
    }

    massivs();

about 4 years ago · Santiago Gelvez 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!