【问题标题】:Filtering Arrays in Javascript [duplicate]在Javascript中过滤数组[重复]
【发布时间】:2017-03-25 10:45:42
【问题描述】:

假设我有以下两个数组:

var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = [];

我想遍历arrTwo,如果它包含一个也在arrOne 中的元素,请将其从arrTwo 中删除,然后将其插入arrThree。所以看看上面的数组,之后数组的状态应该是这样的:

var arrOne = [1, 4, 7];
var arrTwo = [2, 3, 5];
var arrThree = [1, 4];

谁能指出我正确的方向和解决此问题的最佳方法?如果提供了代码,将非常感谢您提供分步说明,以便我了解发生了什么。

【问题讨论】:

标签: javascript


【解决方案1】:

一个简单的for循环,匹配indexOf和拼接匹配。

var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = [];
for (var i = 0; i < arrTwo.length; i++) {
  if (arrOne.indexOf(arrTwo[i]) >= 0) {
    arrThree.push(arrTwo[i]);
    arrTwo.splice(i, 1);
    i--;
  }
}

console.log(arrOne, arrTwo, arrThree)

Array.IndexOf

Array.splice

【讨论】:

    【解决方案2】:

    查看Underscore 库。 arrOne 中也存在于 arrTwo 中的所有元素都称为_.intersection()

    【讨论】:

      【解决方案3】:

      Array#spliceArray#unshift 方法使用简单的while 循环。

      var arrOne = [1, 4, 7];
      var arrTwo = [1, 2, 3, 4, 5];
      var arrThree = [];
      // get length of array
      var l = arrTwo.length;
      // iterate over array from the end
      while (l--) {
        // check value present in arrOne
        if (arrOne.indexOf(arrTwo[l]) > -1)
        // if present then remove and insert it
        // at the beginning of arrThree
          arrThree.unshift(arrTwo.splice(l, 1)[0])
      }
      
      console.log(arrTwo, arrThree);

      【讨论】:

        【解决方案4】:

        您好,您可以使用过滤器功能来过滤数组。 试试下面的代码。

            var arrOne = [1, 4, 7];
            var arrTwo = [2, 3, 5, 1];
            var arrThree = [];
        
            function checkValue(a) {
                return !arrOne.indexOf(a);
            }
        
            function checkValue2(a) {
                return arrThree.indexOf(a);
            }
            function myFunction() {
                arrThree = arrTwo.filter(checkValue);
                document.getElementById("demo").innerHTML = arrThree ;
                arrTwo = arrTwo.filter(checkValue2);
                document.getElementById("demo1").innerHTML = arrTwo;
        
            }
        

        【讨论】:

          【解决方案5】:

          var arrOne = [1, 4, 7];
          var arrTwo = [1, 2, 3, 4, 5];
          
          
          var arrThree = diff(arrOne,arrTwo);//passes the two arrays
          console.log(arrThree);
          
          
          function diff(one, two){
            one.forEach(function(e1){ //iterate through the first array
            
              two.forEach(function(e2){//iterate through second
               
                if(e1 == e2) //checking if elements are equal
                 two.pop(e2);//removing from second array
             
              });
           });
            return two; //returning new array
          }

          可以使用下划线js进行简单的数组操作,差值操作为_.difference([1, 2, 3, 4, 5], [5, 2, 10]);

          【讨论】:

            【解决方案6】:
               var array1 = [1, 2, 3, 4, 5, 6],
               var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
            
              var common = $.grep(array1, function(element) {
               return $.inArray(element, array2) !== -1;
              });
            
              console.log(common); // returns [1, 2, 3, 4, 5, 6];        
            
              array2 = array2.filter(function(obj) {
               return array1.indexOf(obj) == -1;
              });
            
              // returns [7,8,9];
            

            【讨论】:

              【解决方案7】:

              由于数组已排序,您可以并行读取它们:O(n) 而不是 O(n2)。不要为这样一个简单的问题使用库,这太过分了:-)

              var i = 0, j = 0;
              var a = [1, 4, 7];
              var b = [1, 2, 3, 4, 5];
              var c = [];
              
              while (i < a.length && j < b.length) {
                if (a[i] < b[j]) i++;
                else if (a[i] > b[j]) j++;
                else c.push(b.splice(j, 1)[0]);
              }
              
              console.log("a " + toString(a));
              console.log("b " + toString(b));
              console.log("c " + toString(c));
              
              function toString (v) {
                return "[ " + v.join(" ") + " ]";
              }

              追踪:

              #0 init
              
                a = [ 1 4 7 ]
                      i
                b = [ 1 2 3 4 5 ]
                      j
                c = []
              
              #1 a[i] = b[j] => move b[j] to c
              
                a = [ 1 4 7 ]
                      i
                b = [ 2 3 4 5 ]
                      j
                c = [ 1 ]
              
              #2 a[i] < b[j] => increment i
              
                a = [ 1 4 7 ]
                        i
                b = [ 2 3 4 5 ]
                      j
                c = [ 1 ]
              
              #3 a[i] > b[j] => increment j
              
                a = [ 1 4 7 ]
                        i
                b = [ 2 3 4 5 ]
                        j
                c = [ 1 ]
              
              #4 a[i] > b[j] => increment j
              
                a = [ 1 4 7 ]
                        i
                b = [ 2 3 4 5 ]
                          j
                c = [ 1 ]
              
              #5 a[i] = b[j] => move b[j] to c
              
                a = [ 1 4 7 ]
                        i
                b = [ 2 3 5 ]
                          j
                c = [ 1 4 ]
              
              #6 a[i] < b[j] => increment i
              
                a = [ 1 4 7 ]
                          i
                b = [ 2 3 5 ]
                          j
                c = [ 1 4 ]
              
              #7 a[i] > b[j] => increment j
              
                a = [ 1 4 7 ]
                          i
                b = [ 2 3 5 ]
                            j
                c = [ 1 4 ]
              
              #8 j = length of b => done
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-07-19
                • 1970-01-01
                • 2018-01-18
                • 2018-12-07
                • 2022-12-05
                • 2020-02-21
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多