Is there a way to check if two arrays have the same elements?

前端 未结 4 1817
名媛妹妹
名媛妹妹 2021-01-18 04:56

Let say I have 2 arrays

firstArray  = [1, 2, 3, 4, 5];
secondArray = [5, 4, 3, 2, 1];

I want to know if they contain the same elements, whi

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 06:00

    Using jQuery

    You can compare the two arrays using jQuery:

    // example arrays:
    var firstArray  = [ 1, 2, 3, 4, 5 ];
    var secondArray = [ 5, 4, 3, 2, 1 ];
    
    // compare arrays:
    var isSameSet = function( arr1, arr2 ) {
      return  $( arr1 ).not( arr2 ).length === 0 && $( arr2 ).not( arr1 ).length === 0;  
    }
    
    // get comparison result as boolean:
    var result = isSameSet( firstArray, secondArray );
    

    Here is a JsFiddle Demo

    See this question helpful answer

提交回复
热议问题