Compare and filter multidimensional arrays against each other in java script?

I have two multidimensional arrays. The first contains a lot of data, such as:

var dataarray = [ ["AX005A", "Dust / Gas", "1D / 2G", "21", "Ex II 3D IP67"],
["AX007A", "Dust / Gas", "3D / 3G", "22 / 2", "Ex II 3D T60"],
["AX008A", "Dust / Gas", "3D / 3G", "21", "Ex II 3D T60"],
["AX014A", "Dust", "3D", "22", "Ex II 3D T60"],
["AY042A", "Dust", "3D", "20", "Ex II 3D IP65 T65"],
["AY046A", "Dust", "3D", "21", "Ex II 3D T65"]];

The second stores the search queries as follows:

var searchterms = [["1","Dust / Gas"],["3","21"]];

Now I want the results array to contain only data in which the dataarray matches both search engines each at the correct index given by seachterms [n] [0]

For the seachterms parameters above, the result will look like this:

resultarray = [["AX005A", "Dust / Gas", "1D / 2G", "21", "Ex II 3D IP67"],
["AX008A", "Dust / Gas", "3D / 3G", "21", "Ex II 3D T60"]];

Since the number of search engines is not always the same, it should be a general solution that works with 2 search engines, as well as with 4 or 5 search engines.

I tried nested "for loops" with different if conditions, but at some point I got lost :(

, - . , , - jQuery ( )

+3
2

:

function find(haystack, needles) {
    var results = [];
    for(var i = haystack.length; i--;) {
        var matches = true;
        var item = haystack[i];
        for(var j = needles.length; j--; ) {
            var needle = needles[j];
            if(needle[1] !== item[needle[0]]) {
                matches = false;
                break;
            }
        }
        if(matches) {
            results.push(item);
        }
    }
    return results;
}

DEMO ( , ["3","21"], ["4","21"])

( , ). . searchterms {0: "Dust / Gas", 3: "21"}, for

for(var key in needles) {
   if(needles.hasOwnProperty(key)) {
       if(needles[key] !== item[key]) {
           matches = false;
           break;
       }
   }
}
+3

, - . http://jsfiddle.net/cwZz7/3/ .

var dataarray = [ ["AX005A", "Dust / Gas", "1D / 2G", "21", "Ex II 3D IP67"],
["AX007A", "Dust / Gas", "3D / 3G", "22 / 2", "Ex II 3D T60"],
["AX008A", "Dust / Gas", "3D / 3G", "21", "Ex II 3D T60"],
["AX014A", "Dust", "3D", "22", "Ex II 3D T60"],
["AY042A", "Dust", "3D", "20", "Ex II 3D IP65 T65"],
["AY046A", "Dust", "3D", "21", "Ex II 3D T65"]];

var searchterms = [["1","Dust / Gas"],["3","21"]];    
/*
resultarray = [["AX005A", "Dust / Gas", "1D / 2G", "21", "Ex II 3D IP67"],
["AX008A", "Dust / Gas", "3D / 3G", "21", "Ex II 3D T60"]];
*/


var resultArray = new Array();

for(i=0;i< dataarray.length; i++)
{
    var valid = true;
    for(j=0;j < searchterms.length;j++)
    {
          var index = parseInt(searchterms[j][0]);
          if(dataarray[i][index] != searchterms[j][1]){
                 valid = false;
                 break;
          }         

    }
    if(valid == true)
            resultArray.push(dataarray[i]);

}

// resultArray will have your result
0

Source: https://habr.com/ru/post/1793603/


All Articles