Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading