How to compare elements of two JavaScript arrays?

2

I have a checkbox that allows me to filter items according to which are selected, but the system that I am was designed for a select of only 1 value, now I am adapting it to work with arrays instead of a number.

Code

let items = this.backup.filter(p => (
          p.tags == tags
          ));

This code is filtering the items whose array p.tags is equal to tags, this works but only when I select a single tag, because when I select more than one logically it will not be the same and that's why it will not work.

Let's imagine that the array p.tags has this content

p.tags = ["PHP", "Wordpress"]

And the array tags the following content

tags = ["Wordpress", "PHP"]

Already with the simple fact that they are disordered they are no longer the same and therefore this does not work.

In summary I would need something that allows me to match it to arrays that are "Like" and not "Equal", based not on the equality between the arrays but between their contents. I do not know if I explain myself well.

    
asked by Santiago D'Antuoni 29.03.2017 в 22:34
source

4 answers

2

Sure there is some more elegant way of doing it, but ordering both arrays and then comparing each element can get the result.

This element comparison can be done:

  • Using the function every , which iterates over each element of the array.
  • In each iteration , the function every is passed a function that takes the element and the index of that element, and compare the element against the element of the other arrangement in its same position.
  • In addition, it must be compared that the arrays have the same number of elements (with length ).
  • Example:

    array1 = ["uno", "dos", "tres"];    
    array2 = ["tres", "dos", "uno"];
    
    //Se ordenan ambos arreglos
    array1.sort();
    array2.sort();
    
    // y se realiza la comparación de cada elemento
    array1.length==array2.length && array1.every(function(v,i) { return v === array2[i] } );
    
        
    answered by 29.03.2017 в 23:22
    1

    For what you say is a comparison, Here is an example to the rapida.

    var encuentra = false;
    for(var i =0; i < p.tags.length;i++){
        encuentra = false;
        for(var j =0; j < tags.length;j++){
             if(p.tags[i] == tags[j]){
                 encuentra = true;
                 break;
             }
        }
        if(!encuenta){
           alert("los arreglos no son iguales");
           break;
        }
    }
    if(encuentra){
        alert("si son iguales");
    }
    
        
    answered by 29.03.2017 в 23:06
    0

    There are different ways to do it, it may be that in your case it suffices to make sure that all the elements of one array exist in the other.

    let items = this.backup.filter(p => (
        //Sí la longitud no es la misma son diferentes
        p.tags.length === tags.length && p.tags.every( item => tags.includes(item) )
    ));
    
        
    answered by 29.03.2017 в 22:55
    0

    You must use the Array.includes (), in this way you can check if the given array contains each of the elements of the second array through which you iterate with the filter function, if includes === true you can create a new array only with the elements returned by the function.

    //p.tags = ["PHP", "Wordpress"]
    var tags = ["Wordpress", "PHP"]
    var items = tags.filter( function (tag) {
      console.log( tags.includes(tag) )
       
    });
        
    answered by 29.03.2017 в 23:52